是否仅显示已登录的当前用户的帖子?

时间:2011-03-07 作者:janoChen

我使用以下代码仅获取带有post\\u类型问题和当前登录用户的帖子。

但如果没有使用记录,所有帖子都会显示出来。

有什么建议吗?

代码:

<?php global $current_user; ?>
<?php get_currentuserinfo(); ?>

<?php echo \'Username: \' . $current_user->user_login . "\\n"; ?>

<?php
$args = array(
    \'post_type\' => \'question\',
    \'post_author\'  => $current_user->ID)
?>

<?php $custom_posts = new WP_Query(); ?>
<?php $custom_posts->query($args); ?>
<?php while ($custom_posts->have_posts()) : $custom_posts->the_post(); ?>
    <div class="block-2 border-top">
        <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( \'Permalink to %s\', \'twentyten\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        <?php the_excerpt(); ?>
        <p><?php comments_number(\'0 Replies\',\'1 Reply\',\'% Replies\'); ?>.</p>
        <p><?php display_votes(get_the_ID()); ?></p>
        <p><?php the_views(); ?></p>
    <span class="cat-links">
        <?php printf( __( \'<span class="%1$s">Posted in</span> %2$s\', \'twentyten\' ), \'entry-utility-prep entry-utility-prep-cat-links\', get_the_category_list( \', \' ) ); ?>
    </span>
    </div>
<?php endwhile; ?>

1 个回复
最合适的回答,由SO网友:keatch 整理而成

变量不是valorize:$current_user->ID0, 因此,查询将提取所有帖子,尝试为其提供一个不存在的整数值

尝试替换$current_user->ID 具有

($current_user->ID==0) : -1 ? $current_user->ID
如果没有用户,则将-1分配给作者。因此,查询必须不返回任何结果!

结束

相关推荐

如何从wp_list_Authors中排除特定作者

我想让作者像往常一样从wp_list_authors() 但我知道有几个我也想从名单中排除。有办法吗?谢谢