Sometimes you would like to add a specific featured image to post if it belongs to a specific category. You would like the posts from one category to display an image when it does not have an image attachment with itself, so if an author leaves out or forgets a to put a featured image, you don’t have a blank image for that post and a category image is displayed automatically. This can be done very easily in wordpress. Here are the steps

1-First save images or icons for each category with their name. For example for news category save a picture news.png, for economy category save economy.png and so on. Save all of them in images folder of your active theme.

2-Then in your template files where ever you want to show featured images write this code.

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


and you are done!

Another way to add images to category posts is by using plugin. A plugins you can use for this purpose is
Category Thumnail

Yumna Tatheer

I am a Computer Science graduate and a part time blogger from Lahore, Pakistan. I am a PHP programmer and a wordpress expert.

More Posts

Follow Me:
Facebook