现在还完全不清楚你到底想做什么,但根据这一点:
它可以工作,但主循环会生成重复的帖子
我可以告诉你,这是预期产出。
“main”循环查询并显示父页面ID为的所有页面10
. 假设有10个页面与您的“主”查询匹配,这意味着您的循环将运行10个周期。将循环视为foreach
循环,因为它本质上就是这样(但不完全相同)。
现在,在每个周期中,您将添加另一个查询,该查询查询父ID为的页面20
. 假设还有10个。因此,“main”循环的每个周期都将输出10篇文章,文章父级为20
, 10次,因为有10个页面的父级为10
.
非常基本,您有:
// First cycle
the_post(); // 1st page with parent 10
// custom query which will display pages from parent 20
// End of first cycle and start of second cycle
the_post(); // 2nd page with parent 10
// custom query which will display pages from parent 20
// End of second cycle and start of third cycle
the_post(); // 3rd page with parent 10
// custom query which will display pages from parent 20
etc etc
您应该将第二个查询放在“main”查询之外,以将它们分开
$main_args = [
\'post_type\' => \'page\',
\'post_parent\' => \'10\',
];
// execute the main query
$the_main_loop = new WP_Query($main_args);
// go main query
if($the_main_loop->have_posts()) {
while($the_main_loop->have_posts()) {
$the_main_loop->the_post();
// Display your loop content
} // endwhile
wp_reset_postdata(); // VERY VERY IMPORTANT
}
// define the inner query
$inner_args = [
\'post_type\' => \'page\',
\'post_parent\' => \'20\',
];
// execute the inner query
$the_inner_loop = new WP_Query($inner_args);
// go inner query
if($the_inner_loop->have_posts()) {
while($the_inner_loop->have_posts()) {
$the_inner_loop->the_post();
// Display your loop content
} // endwhile
wp_reset_postdata(); // VERY VERY IMPORTANT
}