How to add featured image for each category in wordpress

category featured image

category featured image

Featured image was introduced in wordpress 2.9 as before that theme developers are using different type of techniques to show featured image for each post.

Featured image is used to enhance the value of your post on the front page and it also increase the appeal of that particular post, but as the blog grow it is very hard to search and enable featured image for each and every post.

The simple solution of this problem is, Why not to define a featured image for each category,so that when we are running sort of time to locate a perfect picture for that post, Display a default image assigned for that category.


Step 1: Find out Default pictures for each category

This is one of the toughest part of this whole workout, As most of the time quality images are protected through copyright. Flicker and creative commence are two good options for the same. Decide which photograph best suite for each category.

Step 2 : The Code for featured Image per Category

Open your index.php file and paste the following code inside the main loop. The below code, I have taken from my theme iNivo ,which is available on free of charge at themes section.

[code lang=”php”]</p>
<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail(array(150,150), array("class" => "alignleft post_thumbnail")); }
else {
if(in_category(‘1’)) { /*general category */ ?>
<img src ="<?php bloginfo(‘template_directory’);?>/images/files.jpg" width="150" height="150" alt="General image" />
<?php   }
elseif(in_category(’11’)) { /* home business */ ?>
<img src ="<?php bloginfo(‘template_directory’);?>/images/home-business.jpg" width="150" height="150" alt="pending image" />
<?php }
elseif(in_category(’12’)) { /* Home renovation */ ?>
<img src ="<?php bloginfo(‘template_directory’);?>/images/default_new.jpg" width="150" height="150" alt="pending image" />
<?php }
elseif(in_category(‘102’)) { /* make money online tips and tricks */ ?>
<img src ="<?php bloginfo(‘template_directory’);?>/images/online-money.jpg" width="150" height="150" alt="pending image" />
<?php }
elseif(in_category(’17’)) {  /*search engine optimization */  ?>
<img src ="<?php bloginfo(‘template_directory’);?>/images/seo-tips.jpg" width="150" height="150" alt="SEO Tips image" />
<?php }
elseif(in_category(‘287’)) { /* wordpress tips and tricks */?>
<img src ="<?php bloginfo(‘template_directory’);?>/images/php2.jpg" width="150" height="150" alt="wordpess tips and tricks image" />
<?php }
else { /* otherwise display the following image */?>
<img src ="<?php bloginfo(‘template_directory’);?>/images/default_new.jpg" width="150" height="150" alt="wordpess tips and tricks image" />
<?php }

the_excerpt(”);  ?>
<br />
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">Read More &raquo;</a>

Step 3 : How to implement

Do not forget to copy the image files from your local computer to your theme directory’s image folder.

I took utmost care to implement this feature “Featured image by category”  if you are struck anywhere, Please feel free to ask any question regarding this implementation.

Related Posts

4 thoughts on “How to add featured image for each category in wordpress”

  1. Greetings! I’m curious to know why there’s a tag at the top of the code you’ve provided? Is there a specific PLACE inside the index.php file where the new code should be inserted?

    The only I could find in my index.php is in the comments area of that file. Is it OK for the new code to be placed there? Or should I put it somewhere else?

    Thanks in advance for your help!

  2. sir
    please give me definiation of featured image that what is featured image and why we preffer to input ???

    1. Here in this case we have to see how your theme is rendering the home page. Without code i am not able to guide you in this case. Do you have access to its code? ~rakesh kumar

Leave a Reply

Your email address will not be published.