Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');?>
    </a>
</div>
<?php endif; ?>

Can anybody help?

share|improve this question
add comment

3 Answers 3

<?php
if(has_post_thumbnail())
    the_post_thumbnail('themolio-featured-image');
else
    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.

UPDATE

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

<?php
$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;?>" />
    </a>
</div>
<?php endif; ?>
share|improve this answer
    
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? –  Martin Horvath Dec 9 '12 at 16:11
    
edited the answer –  Mridul Aggarwal Dec 9 '12 at 18:12
add comment

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!

share|improve this answer
add comment

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.

<?php

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" />'; 

    else:

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

    </a>

    <?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() ) :
    wpsites_post_thumbnail();
endif; ?>

Source WP Sites

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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