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;
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题