Can I set a default featured image for a category?

I’m setting up a site with a theme that takes advantage of the “featured image” feature of WordPress. Since my end users are not the most tech saavy in the world, I’d like to set things up so that the categories would have a default “featured image” assigned to the post. In the event of multiple categories assigned to a post, then it would simply take the first one.

Is there an existing plugin or a way to code this?

Related posts

Leave a Reply

5 comments

  1. In your template where you have the Featured Image displayed: <?php the_post_thumbnail( 'thumbnail' ); ?> you can make it conditional on whether the Featured Image is set, then have it default to whatever you want if it’s not set.

    One way to do this is to put all the default images in a directory and name them for the categories, eg. news.jpg and reviews.jpg then instead of using <?php the_post_thumbnail( 'thumbnail' ); ?> to display your Featured Image you’d use this:

    <?php 
       if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { 
          the_post_thumbnail('thumbnail');
       } else { ?>
          <img src="whatever/directory/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /> <?php }
       endif;
    } ?>
    

    So in this example above if the post is in the news category and your writer didn’t set the Featured Image it’ll default to the image stored at http://www.yoursite/whatever/directory/news.jpg.

  2. <?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?>
    
    <?php the_post_thumbnail('thumbnail'); ?>
    
    <?php else :?>
    
    <img src="<?php bloginfo('template_directory'); ?>/your image directory name in theme folder/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" /> 
    
    <?php endif;?>
    
  3. I ran into some problems with category names having spaces i altered the above code a bit to fit my needs

    <?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?>
    
    <?php the_post_thumbnail('thumbnail'); ?>
    
    <?php else :?>
    
    <img src="whatever/directory/<?php $category = get_the_category(); echo $category[0]->cat_ID; ?>.jpg" /> 
    
    <?php endif;?>
    

    basically just change

    echo $category[0]->cat_name;
    

    to

    echo $category[0]->cat_ID;
    

    which will make your image 16.jpg or 3.jpg whatever corresponds with your category number.

  4. A cleaner way to do the above 😉

    if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) : 
        the_post_thumbnail( 'thumbnail' );
    else :
        ?><img src="whatever/directory/<?php 
            $category = get_the_category(); echo $category[0]->cat_name; 
            ?>.jpg" /><?php
    endif;
    
  5. I’ve successfully used Taxonomy Images by Michael Fields – it adds management functions to the category / taxonomy edit page, so that featured images work for taxonomies just as they do with posts/pages. Has all the functions you need for your theming.

    snap of the custom taxonomy admin screen