Conditional post thumbnail based on logged status and post tag
Question
I’d like to show different post thumbnails in archive.php and category.php like this:
- user logged out & post has tag “private” -> shows a “default-private” image
- user logged in & post has tag “private” -> show the actual featured image attached to the post
- no matter the logged in status and all other posts with no “private” tag, if no image set -> show “default” image
This is what I’ve come up with so far:
<?php if (!is_user_logged_in() && has_tag('private')) { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<img src="<?php bloginfo('template_directory'); ?>/img/default-login.jpg" alt="<?php the_title(); ?>" />
</a>
<?php } else {
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail('news-thumb',
array('class' => 'news-thumb'));
} else { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<img src="<?php bloginfo('template_directory'); ?>/img/default.jpg" alt="<?php the_title(); ?>" />
</a>
<?php } ?>
This is not working.
0
archives, post-thumbnails, user-roles
3 years
2020-06-04T08:10:38-05:00
2020-06-04T08:10:38-05:00 0 Answers
80 views
0
Leave an answer