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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值