我需要2个帖子,但是只显示1个-WP_Query

时间:2018-11-12 06:02:14

标签: wordpress

我正在使用此wp_query。我想在侧边栏上显示2个帖子,但是,它只显示一个-循环有任何错误的配置吗?谢谢!

The content is on a row of bootstrap and the sidebar have 3 cols

                       <?php                            
                        $destaque = new WP_Query('post_type=post&posts_per_page=1&cat=2,3,4,5');
                        if($destaque->have_posts()):
                            while($destaque->have_posts()):
                                $destaque->the_post();
                        ?>
                            <div class="col-md-12">
                                <?php get_template_part('content','homepost'); ?>
                            </div>

                        <?php
                            endwhile; wp_reset_postdata();
                        endif;
                         ?>

                        <?php  
                        $args = array(
                            'post_type'         =>  'post',
                            'posts_per_page'    =>  2,
                            'category__not_in'  =>  array(1),
                            'category__in'      =>  array(2,3,4,5),
                            'offset'            =>  1
                        );

                        $secundarias = new WP_Query($args);

                        if($secundarias->have_posts()):
                            while($secundarias->have_posts()):
                                $secundarias->the_post();
                        ?>

                        <?php
                            endwhile;
                             wp_reset_postdata();
                        endif;

                         ?>

1 个答案:

答案 0 :(得分:0)

不知道为什么会有两个查询,但是只是这样

$new_query = new WP_Query();
$new_query->query('post_type=post&showposts=2');

或在您的示例中

('posts_per_page=2');

在第一个查询中,将其设置为“ 1”。
在循环之前使用单个查询,它应该可以工作。

还要检查您的类别-有时只是

相关问题