原因:粘性首页代码,显示最新的非粘性登录首页

时间:2013-02-17 作者:Django

我有这个代码,只在首页显示最新的粘性帖子。

奇怪的是,当我登录时,它会显示非粘性的最新帖子,即使它是私人的。。。

<?php get_header(); ?>
<?php if ( is_home() && !is_paged() ) : ?>

<?php
    // Get IDs of sticky posts
    $sticky = get_option( \'sticky_posts\' );
    // first loop to display only my single, 
    // MOST RECENT sticky post
    $most_recent_sticky_post = new WP_Query( array( 
    // Only sticky posts
    \'post__in\'            => $sticky, 
    // Treat them as sticky posts
    \'ignore_sticky_posts\' => 1, 
    // Order by date to get the most recently published sticky post
    \'orderby\'             => date, 
    // Get only the one most recent
    \'posts_per_page\'      => 1
    ) );
?>

<?php while ( $most_recent_sticky_post->have_posts() ) :  $most_recent_sticky_post->the_post(); ?>
<!-- your code to display most recent sticky -->

<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
这个想法是,那些登录的人看到的东西与那些未登录的人看到的东西完全相同。

有人知道怎么表达吗?

1 个回复
SO网友:s_ha_dum

如果您希望登录用户和未登录用户都看到相同的内容,那么您只希望看到“已发布”的帖子状态,至少对我来说,向未登录用户显示“私有”帖子没有意义。更改查询以包含该条件。

$most_recent_sticky_post = new WP_Query( 
  array( 
    \'post_status\' => array( \'publish\' ),
    // Only sticky posts
    \'post__in\'            => $sticky, 
    // Treat them as sticky posts
    \'ignore_sticky_posts\' => 1, 
    // Order by date to get the most recently published sticky post
    \'orderby\'             => date, 
    // Get only the one most recent
    \'posts_per_page\'      => 1
    ) 
);
ignore_sticky_posts, 什么时候true 正如您所拥有的,将粘性帖子视为普通帖子,这不是您的代码注释所说的,但我认为这就是您的意思。

结束