要在WP_Query中包括孩子,可以使用'post_parent'参数来筛选具有特定父级的帖子。以下是一个示例代码,演示如何包括孩子在WP_Query中:
$args = array(
'post_type' => 'post', // 帖子类型
'post_parent' => 0, // 父级帖子ID,0表示顶级帖子
'posts_per_page' => -1, // 显示所有帖子
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// 输出帖子的标题、内容等
the_title();
the_content();
// 获取当前帖子的子帖子
$child_args = array(
'post_type' => 'post',
'post_parent' => get_the_ID(), // 当前帖子的ID作为父级帖子
'posts_per_page' => -1, // 显示所有帖子
);
$child_query = new WP_Query($child_args);
if ($child_query->have_posts()) {
while ($child_query->have_posts()) {
$child_query->the_post();
// 输出子帖子的标题、内容等
the_title();
the_content();
}
}
wp_reset_postdata(); // 重置查询
}
}
在上面的示例中,我们首先使用'post_parent'参数将为0的父级帖子查询出来。然后,我们循环遍历每个父级帖子,并使用'get_the_ID()'函数获取每个父级帖子的ID。接下来,我们使用这个ID作为'post_parent'参数来查询具有该父级的子帖子。然后,我们可以输出子帖子的标题、内容等。在完成每个父级帖子和子帖子的循环后,我们使用'wp_reset_postdata()'函数重置查询。
下一篇:包括航向差异的地理空间距离度量