在 wordpress 的父帖子页面上显示子帖子 [英] Display child post on parent post page in wordpress

查看:28
本文介绍了在 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.

我在这里和那里找到了一些想法,但到目前为止似乎都没有奏效.(例如: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).我不知道这是否与我使用的是帖子而不是页面的事实有关.

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.

到目前为止,这是我在 single.php 中尝试过的:

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_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.

推荐答案

好的,在循环内的帖子模板中尝试类似的操作.它应该可以帮助您在特定帖子中输出子帖子.循环中的某个地方/

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();
?>

更新:好的,你可以尝试使用 post_parent__in &ID 数组,这也应该有效.循环中的某个地方/

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();
?>

如果没有,这里是使用 get_children 函数输出帖子内容的方法.这应该也可以工作

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;
    }
?>

这篇关于在 wordpress 的父帖子页面上显示子帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆