Adding Category Images To posts

Home/Wordpress tricks/Adding Category Images To posts

Adding Category Images To posts

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

Yamna Tatheer is a WordPress Plugin developer and a Top-Rated Upwork professional. She is a part-time blogger from Lahore and loves to write about wordpress and freelance industry.

More Posts

Follow Me:
Facebook

About the Author:

Yamna Tatheer is a Wordpress Plugin developer and a Top-Rated Upwork professional. She is a part-time blogger from Lahore and loves to write about wordpress and freelance industry.

Leave A Comment