7

I'm want to set a featured image for the archive templates of my theme. To my knowledge, there is not a way to do so in the WordPress Admin (like Pages and Posts).

I was thinking about writing a function that filters through the currently uploaded featured images and matches the template to the file name. Might be a bit complex.

I also know there are theme options. But this seems like a lot to manage.

How would you recommend doing this that facilitates Featured Image administration (Theme -> Header) and ease of maintenance...

3
  • Just to clarify: are you trying to associate an image with an archive.php template/page of results? Sep 21, 2012 at 0:21
  • Yes, in a nutshell I'm trying to associate featured images with archive templates. Sep 21, 2012 at 2:47
  • I don't really get it. Do you want to set a header image for the archive template? If so, it would be as simple as if( is_archive() ) {..your image..}. If you want to change it from the backend, then you indeed need to create a options page for it. (Maybe there are plugins to do this, idk)
    – xsonic
    Sep 21, 2012 at 12:36

6 Answers 6

1

There are lots of archives in WordPress. Tag archives, date archives, category archives, term archives, author archives. Which archive do you mean?

You could start by trying the Taxonomy Images plugin http://wordpress.org/extend/plugins/taxonomy-images/

1

This is old, but it's the first search that comes up on Google under this topic.

The way I found round this was to create a new page. Don't give it the same name as your archive page (Wordpress doesn't like pages with the same name as your archive page it causes problems), you can call it anything else. Set the featured image and then call it on your archive template using this code:

$post_id = 8; 
$queried_post = get_post($post_id);
$src = wp_get_attachment_image_src(get_post_thumbnail_id($queried_post->ID), '') ;
$thumb_id = get_post_thumbnail_id($post_id);
?>
<img src="<?php echo $src[0]; ?>" class="img-responsive" />

To break it down:

$post_id = 8;

is the id number of your new page.

$src = wp_get_attachment_image_src(get_post_thumbnail_id($queried_post->ID), '') ; 

finds the featured image attached to this page

<img src="<?php echo $src[0]; ?>" class="img-responsive" /> 

echos out the featured image from your new page.

0

you could do this via css by defining an header image region in your archive.php and setting a background image in your style.css

or you could use the plugin dynamic headers http://wordpress.org/extend/plugins/dynamic-headers/ (works great even though it hasn't been updated since 2010)

0

Coming in quite late on this, but I believe this plugin addresses the original question: http://wordpress.org/plugins/categories-images/

It allows an admin to select a 'featured image' for a particular category / tag / taxonomy on the taxonomy edit panel. Then it can be used in a theme's archive.php (for example) by echoing z_taxonomy_image_url();.

If you'd rather roll your own, maybe check out the code in that plugin for a starting point.

0

I think you should go with Manual method, something like this:

  1. enter each category "EDIT" page, and in the description, place like this:
    [image url="https://upload.wikimedia.org/wikipedia/commons/0/0c/Bunker_in_Albanian_Alps.jpg"]
  1. get image for category:
    if (is_archive())   {
        if (!empty($GLOBALS['wp_query']->queried_object->description) )  { 
            $cat_description =$GLOBALS['wp_query']->queried_object->description); 
                //p.s. from other pages, you can get category description by:  get_term('1533' , 'category')->description;  
            preg_match('/url\=\"(.*?)\"/si', $cat_description, $new);
            if(!empty($new[1])) {
                $found_image= $new[1];
            }
        }
    }

now, you can use that image anywhere, in head or in page...

0

YOAST! Premium does this out-of-the-box.

However, I use ACF, where I can create an options page, where I added a repeater field where I can set a featured image for each post type.

Then, with the function below added to my functions.php, I call the necessary meta tags on the post type archives for which I defined a featured image.

function custom_featured_archive_image() {
    $o = get_field("featured_images", "option");
    if (is_array($o)) {
        foreach($o as $option) {
            if (is_post_type_archive($option["post_type"])) {
                $image_id = $option["image"];
                $image_url = wp_get_attachment_image_src($image_id, "original");
                $mime_type = get_post_mime_type($image_id);
                
                echo '
                    <meta property="og:image" content="'.$image_url[0].'">
                    <meta property="og:image:width" content="'.$image_url[1].'">
                    <meta property="og:image:height" content="'.$image_url[2].'">
                    <meta property="og:image:type" content="'.$mime_type.'">
                    <meta name="twitter:image" content="'.$image_url[0].'">
                ';

            }
        }
    }
}
add_action('wp_head', 'custom_featured_archive_image', 0);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.