Recent posts on homepage: different thumbnail based on logged in status and post category
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
divtime 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 gt;/a' );
echo $trimmed_content;
?
/div
/div
Topic recent-posts post-thumbnails user-roles get-posts Wordpress
Category Web