admin管理员组文章数量:1292809
This works well on archive.php
, but not working on the homepage. For some reason, the first statement doesn't get applied: if a post is in Members category and user is not logged in, it's supposed to show a default placeholder image, but instead it's still showing the actual post thumbnail.
<?php if (!is_user_logged_in() && in_category('members')) { ?>
<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() || !in_category('members')) { ?>
<a href="<?php echo get_permalink($post_item['ID']) ?>">
<?php echo get_the_post_thumbnail($post_item['ID'], 'news-thumb'); ?>
</a>
<?php } 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 } ?>
I'm using:
<?php
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 6, // Number of recent posts to display
'post_status' => 'publish' // Show only the published posts
));
foreach( $recent_posts as $post_item ) : ?>
Final version so far:
<?php
$recent_posts = wp_get_recent_posts( array(
'numberposts' => 6, // Number of recent posts to display
'post_status' => 'publish' // Show only the published posts
), OBJECT ); // 1. Set the second parameter to OBJECT.
global $post; // 2. Access the global $post variable.
foreach ( $recent_posts as $post ) : // 3. Rename $post_item to $post
setup_postdata( $post ); // 4. Set up global post data. ?>
<div>
<?php if (!is_user_logged_in() && in_category('members')) { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php bloginfo('template_directory'); ?>/img/default-login.jpg" alt="<?php the_title(); ?>" />
</a>
<?php }
else if (is_user_logged_in() || !in_category('members')) { ?>
<a href="<?php echo get_permalink($post_item['ID']) ?>">
<?php echo get_the_post_thumbnail($post_item['ID'], 'news-thumb'); ?>
</a>
<?php } else { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php bloginfo('template_directory'); ?>/img/default.jpg" alt="<?php the_title(); ?>" />
</a>
<?php } ?>
<a rel="bookmark" href="<?php echo get_permalink($post_item['ID']) ?>" title="<?php the_title(); ?>">
<h6><?php the_title(); ?></h6>
</a>
<div><time datetime="<?php echo get_the_date('c'); ?>">
<?php echo get_the_date(); ?></time>
</div>
<div>
<?php
$content = get_the_content();
$content = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $content); # strip shortcodes, keep shortcode content
$trimmed_content = wp_trim_words( $content, 14, '... <br /><a href="'. get_permalink() .'">Continue reading ></a>' );
echo $trimmed_content;
?>
</div>
</div>
This works well on archive.php
, but not working on the homepage. For some reason, the first statement doesn't get applied: if a post is in Members category and user is not logged in, it's supposed to show a default placeholder image, but instead it's still showing the actual post thumbnail.
<?php if (!is_user_logged_in() && in_category('members')) { ?>
<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() || !in_category('members')) { ?>
<a href="<?php echo get_permalink($post_item['ID']) ?>">
<?php echo get_the_post_thumbnail($post_item['ID'], 'news-thumb'); ?>
</a>
<?php } 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 } ?>
I'm using:
<?php
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 6, // Number of recent posts to display
'post_status' => 'publish' // Show only the published posts
));
foreach( $recent_posts as $post_item ) : ?>
Final version so far:
<?php
$recent_posts = wp_get_recent_posts( array(
'numberposts' => 6, // Number of recent posts to display
'post_status' => 'publish' // Show only the published posts
), OBJECT ); // 1. Set the second parameter to OBJECT.
global $post; // 2. Access the global $post variable.
foreach ( $recent_posts as $post ) : // 3. Rename $post_item to $post
setup_postdata( $post ); // 4. Set up global post data. ?>
<div>
<?php if (!is_user_logged_in() && in_category('members')) { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php bloginfo('template_directory'); ?>/img/default-login.jpg" alt="<?php the_title(); ?>" />
</a>
<?php }
else if (is_user_logged_in() || !in_category('members')) { ?>
<a href="<?php echo get_permalink($post_item['ID']) ?>">
<?php echo get_the_post_thumbnail($post_item['ID'], 'news-thumb'); ?>
</a>
<?php } else { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php bloginfo('template_directory'); ?>/img/default.jpg" alt="<?php the_title(); ?>" />
</a>
<?php } ?>
<a rel="bookmark" href="<?php echo get_permalink($post_item['ID']) ?>" title="<?php the_title(); ?>">
<h6><?php the_title(); ?></h6>
</a>
<div><time datetime="<?php echo get_the_date('c'); ?>">
<?php echo get_the_date(); ?></time>
</div>
<div>
<?php
$content = get_the_content();
$content = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $content); # strip shortcodes, keep shortcode content
$trimmed_content = wp_trim_words( $content, 14, '... <br /><a href="'. get_permalink() .'">Continue reading ></a>' );
echo $trimmed_content;
?>
</div>
</div>
Share
Improve this question
edited May 11, 2021 at 12:32
Phantasmix
asked May 11, 2021 at 0:34
PhantasmixPhantasmix
1501 silver badge15 bronze badges
4
|
1 Answer
Reset to default 1If you want to use the the_
functions (e.g. the_permalink()
and the_title()
) and including in_category()
(without specifying the second parameter) outside of The Loop (the standard one which calls the_post()
), you should access the global $post
variable and call setup_postdata()
to set up global post data and after your foreach
ends, call wp_reset_postdata()
to restore the $post
back to the current post in the main query.
You also must rename the $post_item
variable to $post
and I'd also use OBJECT
as the second parameter value for wp_get_recent_posts()
, although you could actually do setup_postdata( (object) $post )
.
Working Example
$recent_posts = wp_get_recent_posts( array(
'numberposts' => 6, // Number of recent posts to display
'post_status' => 'publish' // Show only the published posts
), OBJECT ); // 1. Set the second parameter to OBJECT.
global $post; // 2. Access the global $post variable.
foreach ( $recent_posts as $post ) : // 3. Rename $post_item to $post
setup_postdata( $post ); // 4. Set up global post data.
// ... your code here ..
echo ( in_category( 'members' ) ? 'In' : '<b>Not</b> in' ) . ' Members category';
the_title( '<h3>', '</h3>' );
endforeach;
wp_reset_postdata(); // 5. Restore the $post variable.
Remember, you need to change the $post_item
in your code to $post
and use the "arrow" to access the post data, e.g. $post->ID
and not $post['ID']
! :)
本文标签: user rolesRecent posts on homepage different thumbnail based on logged in status and post category
版权声明:本文标题:user roles - Recent posts on homepage: different thumbnail based on logged in status and post category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741562207a2385515.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var_dump( get_post(), in_category( 'members' ), is_user_logged_in() );
somewhere and see if the data are correct for the current post and user. – Sally CJ Commented May 11, 2021 at 1:22<?php $recent_posts = wp_get_recent_posts(array( 'numberposts' => 6, // Number of recent posts to display 'post_status' => 'publish' // Show only the published posts )); foreach( $recent_posts as $post_item ) : ?>
– Phantasmix Commented May 11, 2021 at 1:50