从wp_query循环wordpress中排除一个帖子

时间:2013-02-07 14:06:07

标签: wordpress wordpress-theming

我在包含此代码的页面中显示我的上一篇文章:

$query1 = new WP_Query();
$query1->the_post();

并进一步:

$id = $query->ID;

检索上一篇文章ID 所以我写了一个新的wp_query,我想从结果中删除该ID: 我写了这个,但它不起作用:

$query2-> new WP_Query('p=-$id');

问题是什么?

2 个答案:

答案 0 :(得分:2)

你没有排除任何东西。 Read the Codexp= 包含帖子。它不排除它们。您需要的是post__not_in

$query2-> new WP_Query(array('post__not_in' = array($id)));

答案 1 :(得分:1)

我的代码运行正常:

$ID =array('1,2,3,4,5');
$news = new WP_Query(array('
    'post_type'    => 'post',
    'showposts'    =>3,
    'order'        => 'DESC',
    'post__not_in' => $ID
));
if ( $news->have_posts() ) :
    echo '<div>';
        while ( $news->have_posts() ) : $news->the_post(); ?>`

            //Your code here
相关问题