I´d like to set specific featured image for all posts in one category.

So, if I have category Good Jokes, all posts in category Good Jokes will have the same featured image displayed in front page.

My current code in content.php is:

<?php if(trim(get_the_post_thumbnail($post->ID)) != '' and $themolio_options['show_featured']): ?>
<div class="entry-thumb">
    <a href="<?php the_permalink(); ?>">
    <?php the_post_thumbnail('themolio-featured-image');?>
<?php endif; ?>

Can anybody help?

3 Answers 3

    echo wp_get_attachment_image($attachment_id, 'themolio-featured-image');

The above code displays the featured image if the post has one, otherwise the image define by $attachment_id.

I don't know how you're deciding which category term should display which image & what to do in case of multiple categories, so the part where you initialize $attachment_id is left to you.


This code does exactly what you said in your comment. In case of multiple categories, this code will just take the first one.

$category = get_the_category();
if(!empty($category) && $themolio_options['show_featured']): ?>
<div class="entry-thumb">
    <a href="<?php the_permalink(); ?>">
    <img src="http://example.com/<?php echo $category[0]->term_id;?>.jpg" alt="<?php echo $category[0]->name;?>" />
<?php endif; ?>
  • Thanks. I am not very good in codes. If I specify my conditions, would you be able to you help me? Each post has only one category, so no multiple categories. For example: I have category Good Jokes (ID 19) and I want example.com/19.jpg image for this category. I have about 10 categories and I want specific image for each. What should I edit / add to code? Commented Dec 9, 2012 at 16:11
  • edited the answer Commented Dec 9, 2012 at 18:12

Use FPW Category Thumbnails plugin. It does exactly what you need and no coding is required. Map images to their respective categories. If you have many posts already use Apply Mapping button and all posts belonging to the category which has an image mapped to it will get this image as a thumbnail. From this point on everything is automated. When you create or edit a post it will get its thumbnail according to the plugin's mapping on save. As an author of this plugin I'm sure you will not be disappointed!


Would need to see the unmodified code from your themes content.php file to provide a tested solution which works on your specific theme. Here's a solution i tested on a child theme of Twenty Twelve.


function wpsites_post_thumbnail() {

    if ( in_category('category-first') ) : ?>

    <a class="post-thumbnail" href="<?php the_permalink(); ?>">

    <?php echo'<img src="' . get_stylesheet_directory_uri() . '/images/category-first.png" />'; 

    elseif ( in_category('category-second') ) :

    echo'<img src="' . get_stylesheet_directory_uri() . '/images/category-second.png" />'; 


    echo'<img src="' . get_stylesheet_directory_uri() . '/images/category-default.png" />'; ?>


    <?php endif; 

The above code goes in your functions file and creates a template tag you can replace in your content.php file

<header class="entry-header">
<?php if ( ! post_password_required() && ! is_attachment() ) :
endif; ?>

Source WP Sites

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.