我尝试使用多个wordpress查询来显示特定页面上的特定帖子,问题是,一旦我进行查询,我就无法再执行另一个查询。例如:
$posts=new WP_Query($args = array ('posts_per_page'=>'1','tag_name'=>'frontpage1'));
while ( $posts->have_posts() ) {
$posts->the_post();
$arr['big']=array('id'=>get_the_id());
}
foreach(array(2,3,4,5) as $n){
wp_reset_postdata();
wp_reset_query();
$posts=new WP_Query($args = array ('posts_per_page'=>'1','tag_name'=>'frontpage'.$n));
while ( $posts->have_posts() ) {
$posts->the_post();
$arr['small'][$n]=array('id'=>get_the_id());
}
}
给我这个$ arr:
[big] =>
[id] => 56714
[small] =>
[2] => [id] => 56714
[3] => [id] => 56714
[4] => [id] => 56714
[5] => [id] => 56714
第一个"大"是正确的,但后续查询只是循环相同的结果。如何重置查询?
答案 0 :(得分:0)
在每个查询之间添加wp_reset_postdata();
:
$posts=new WP_Query($args = array ('posts_per_page'=>'1','tag_name'=>'frontpage1'));
while ( $posts->have_posts() ) {
$posts->the_post();
$arr['big']=array('id'=>get_the_id());
}
wp_reset_postdata();
foreach(array(2,3,4,5) as $n){
$posts=new WP_Query($args = array ('posts_per_page'=>'1','tag_name'=>'frontpage'.$n));
while ( $posts->have_posts() ) {
$posts->the_post();
$arr['small'][$n]=array('id'=>get_the_id());
}
}
答案 1 :(得分:0)
试试这个:
$posts=new WP_Query($args = array ('posts_per_page'=>'1','tag'=>'frontpage'.$n));
来源: WP_Query tags