admin管理员组文章数量:1334869
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.
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.
Share Improve this question asked Jun 4, 2020 at 12:24 PhantasmixPhantasmix 1501 silver badge15 bronze badges1 Answer
Reset to default 1Hey you have to use the below logic to handle the above conditions, it will work.
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 if( is_user_logged_in() && has_tag('private') ) {
// show the actual featured image
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail('news-thumb',
array('class' => 'news-thumb'));
}
//if you want to show the default image if featured image is not attached you can add here in else
}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
}
本文标签: user rolesConditional post thumbnail based on logged status and post tag
版权声明:本文标题:user roles - Conditional post thumbnail based on logged status and post tag 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742375078a2462999.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论