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="https://wordpress.stackexchange.com/questions/75455/<?php the_permalink(); ?>">
<?php the_post_thumbnail('themolio-featured-image');?>
</a>
</div>
<?php endif; ?>
Can anybody help?
<?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="https://wordpress.stackexchange.com/questions/75455/<?php the_permalink(); ?>">
<img src="https://example.com/<?php echo $category[0]->term_id;?>.jpg" alt="<?php echo $category[0]->name;?>" />
</a>
</div>
<?php endif; ?>