我们当前的网站使用带有父/子帖子的自定义帖子。
查看(父)帖子时,会使用插件来拉取其子帖子,并将这些子帖子显示在页面上的选项卡中。
我们现在在多个网站上使用该自定义主题的新版本,并且不再使用父/子关系。相反,我们在自定义帖子类型中有元框,所有附加信息都可以在那里归档。
我想用最新版本的主题更新这个特定的网站,但由于它使用父/子关系,我想向主题添加几行代码,以达到相同的结果并保持旧帖子的方式而不是全部修改它们。
这就是我想要做的:我想要一种非常简单的方法来在父帖子页面上显示所有子帖子(按顺序)。
我到处发现了一些想法,但到目前为止似乎没有一个可行。 (这里有一个例子:http://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/ http://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/还有这个https://wordpress.stackexchange.com/questions/153042/how-to-display-list-of-child-pages-with-parent-in-wordpress https://wordpress.stackexchange.com/questions/153042/how-to-display-list-of-child-pages-with-parent-in-wordpress)。我不知道这是否与我使用帖子而不是页面有关。
我不需要子帖子的列表,而是直接显示这些内容的内容。我认为实现此目的的最佳方法可能是创建一个函数来检索子帖子,然后在模板中回显结果。这样,无需更改我们的主题,它就可以与我们不同的网站一起使用。
EDIT :
到目前为止,这是我在 single.php 中尝试过的:
$query = new WP_Query( array(
'post_parent' => get_the_ID(),
));
while($query->have_posts()) {
$query->the_post();
the_content(); //Outputs child's content as it is
}
wp_reset_query();`
然后我将代码更改为:
$new_args = array(
'order' => 'ASC',
'post_parent' => get_the_ID()
);
$new_query = new WP_Query( $new_args);
if ($new_query->have_posts() ) {
while($new_query->have_posts() ) {
$new_query->the_post();
the_content();
}
wp_reset_query();
}
然后由于它不起作用,我将其更改为:
$children = get_children( array('post_parent' => get_the_ID()) );
foreach ( $children as $children_id => $children ) {
the_title();
the_content();
}
最新的似乎能够返回一些结果,它“知道”当前帖子中有子项,但我正在显示当前帖子的标题和内容。我很确定我不应该使用the_content()
在这里。