首页最近的帖子:根据登录状态和帖子类别的不同缩略图

时间:2021-05-11 作者:Phantasmix

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 &gt;</a>\' );            
        echo $trimmed_content;
            ?>
        </div>
</div>
                            
1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

如果要使用the_ 功能(例如。the_permalink()the_title()) 并包括in_category() (不指定第二个参数)循环外部(调用the_post()), 您应该访问全局$post 变量和调用setup_postdata() 要设置全局post数据,请在foreach 结束,通话wp_reset_postdata() 要恢复$post 返回到主查询中的当前帖子。

您还必须重命名$post_item 变量至$post 我也会使用OBJECT 作为的第二个参数值wp_get_recent_posts(), 虽然你真的可以setup_postdata( (object) $post ).

工作示例

$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.
记住,您需要更改$post_item 在您的代码中$post 使用;箭头“;访问post数据,例如。$post->ID 而不是$post[\'ID\']! :)

相关推荐