Can I set a default featured image for a category?

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="https://wordpress.stackexchange.com/questions/5013/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.

Leave a Comment