dongren9966
2017-01-17 20:01
浏览 49
已采纳

在wordpress中显示父帖子页上的子帖子

Our current website is using custom posts with parent/child posts. When viewing a (parent) post, a plugin is used to pull its child posts and those are displayed in a tab on the page.

We are using a new version of that custom theme on several websites now and are no longer using parent/child relationship. Instead we have metaboxes in our custom posts types and all additional information can be filed right there.

I wanted to update this particular website with the latest version of the theme, but since it's using parent/child relationship I wanted to add a few lines of code to the theme in order to achieve the same result and keep the old posts the way they are instead of modifying them all.

Here's what I want to do : I want a very simple way to display all child post (in order) on a parent post page.

I've found a few ideas here and there but none seems to be working so far. (Here's one for example : http://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/ as well as this one https://wordpress.stackexchange.com/questions/153042/how-to-display-list-of-child-pages-with-parent-in-wordpress). I don't know if it has to do with the fact that i'm using post rather than pages.

I don't want a list of the child post but rather display the content of those directly. Was thinking the best way to achieve this might be to create a function to retrieve the child post and then echoing the result in the template. That way without having to change our theme it could work with our different website.

EDIT :

So far here's what i've tried within 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();`  

I then changed the code to :

$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();
}

and then since it didn't work either i've changed it to :

$children = get_children( array('post_parent' => get_the_ID()) );
foreach ( $children as $children_id => $children ) {
    the_title();
    the_content();
}

The latest seems to be able to return some results, it "knows" that the current post has children in it but i'm displaying the title and content of the current post. I'm pretty sure I shouldn't be using the_content() in here.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsaf32131 2017-01-17 20:54
    已采纳

    Ok, try something like this inside your post template within the loop. It should help you to output child posts inside particular post. Somwhere in the loop/

        $query = new WP_Query( array( 
            'post_parent' => get_theID(),
            '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();
    ?>
    

    UPDATED: Ok, you can try to use post_parent__in & array of IDs, this should work too. Somwhere in the loop/

        $query = new WP_Query( array( 
            'post_parent__in' => array(get_theID()),
            '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();
    ?>
    

    If not, here is the way to output contents of the posts you got using get_children function. This should probably work too

    <?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;
        }
    ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题