在WordPress中显示父级文章页面上的子文章。

3
我们当前的网站使用带有父子关系的自定义文章类型。在查看父级文章时,使用插件来获取其子文章,并将其显示在页面的选项卡中。
我们现在在几个网站上使用了新版本的自定义主题,不再使用父/子关系。相反,我们在自定义文章类型中使用元框,所有附加信息都可以在那里填写。
我想要更新这个特定网站的主题到最新版本,但由于它使用父/子关系,所以我想添加一些代码行到主题中,以便实现相同的结果并保持旧的文章不变,而不是修改它们。
这是我想做的:我想要一种非常简单的方法,在父级文章页面上按顺序显示所有子文章。
我找到了一些想法,但目前没有一个似乎有效。(例如: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)。我不知道是否与我使用的是文章而不是页面有关。
我不想要子文章的列表,而是直接显示它们的内容。认为实现这一点的最佳方法可能是创建一个函数来检索子文章,然后在模板中回显结果。这样,即使不更改我们的主题,它也可以与我们的不同网站一起使用。
编辑:
到目前为止,在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()


你可能需要使用WP_Query,并将metaquery作为附加参数来选择具有特定元数据的文章(如果我理解正确,您现在将此parentchild逻辑存储在metaboxes中,是吗?)。除非您至少展示一些代码/ metabox结构或自定义帖子结构,否则我们无法帮助您。 - Codeartist
其实我不希望在元框中显示内容,我只是想简单地在父文章页面上显示子文章内容。我可能在元框中提供了过多的信息,导致有些混乱。我正在使用默认的文章类型,但已经对其进行了一些自定义设置,因此可能不被认为是自定义文章类型。我只想在父文章中显示来自子文章的“常规”内容。 - Sway Raines
2个回答

4

好的,尝试在循环内部的文章模板中添加以下代码。这将帮助您在特定文章中输出子文章。 <?php / 在循环中某处 /

    $query = new WP_Query( array( 
        'post_parent' => get_the_ID(),
        'posts_per_page' => 3, //shows only 3 children. If you want to show all of them, comment this line
    ));
    while($query->have_posts()) {
        $query->the_post();
        /*Output the child markup here*/
        the_content(); //Outputs child's content as it is
    }
    wp_reset_query();
?>

更新:好的,你可以尝试使用post_parent__in和ID数组,这也应该有效。

<?php /在循环中的某个地方/
    $query = new WP_Query( array( 
        'post_parent__in' => array(get_the_ID()),
        'posts_per_page' => 3, //shows only 3 children. If you want to show all of them, comment this line
    ));
    while($query->have_posts()) {
        $query->the_post();
        /*Output the child markup here*/
    }
    wp_reset_query();
?>

如果没有,这里是使用get_children函数获取文章内容并输出的方法。这应该也可以正常工作。
<?php
    $children = get_children( array('post_parent' => get_the_ID()) );
    foreach ( $children as $children_id => $child ) {
        echo $child->post_title;
        echo str_replace( ']]>', ']]&gt;',apply_filters( 'the_content', $child->post_content )); //mimic the_content() filters
        //echo $child->post_content; // if you do not need to filter the content;
    }
?>

我尝试在循环中使用您的代码,但即使我知道此帖子有一个子级,它也没有返回任何内容。 - Sway Raines
你是在single.php中使用它吗?它通常用于输出单个文章。你能给我看看你的代码吗? - Codeartist
我已经编辑了原帖以显示我尝试过的代码。我相信这是一些愚蠢的错误,但我似乎无法让它工作。感谢您的帮助。 - Sway Raines
上一个回答对我有很大帮助!谢谢。 - Sway Raines

0
/* Fetch only one level child pages of a parent page */    
         $pages = get_pages(
         array (
                  'parent'  => 'parent_id', 
                  'post_type  => 'page', 
                  'post_status'  => 'publish',
                );
                
         $ids = wp_list_pluck( $pages, 'ID' );
 /* Return page IDs in array format */
 /* You can retrieve "post_title", "guid", "post_name" instead of "ID"

/* Fetch all child pages of a parent page*/    
$pages = get_pages(
         array (
         'parent'  => '-1',
         'child_of' => 'parent_id', /* Return child of child for current page id */
         'post_type  => 'page',
         'post_status'  => 'publish',
          );
         $ids = wp_list_pluck( $pages, 'ID' );
/* Return page IDs in array format */
/* You can retrieve "post_title", "guid", "post_name" instead of "ID" */

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接