【问题标题】:Display child post on parent post page in wordpress在 wordpress 的父帖子页面上显示子帖子
【发布时间】:2017-01-17 20:01:41
【问题描述】:

我们当前的网站正在使用带有父/子帖子的自定义帖子。 查看(父)帖子时,插件用于拉取其子帖子,并显示在页面上的标签中。

我们现在在多个网站上使用该自定义主题的新版本,并且不再使用父/子关系。相反,我们在自定义帖子类型中有元框,所有其他信息都可以在那里归档。

我想用最新版本的主题更新这个特定的网站,但由于它使用父/子关系,我想在主题中添加几行代码以达到相同的结果并保留旧帖子它们的方式而不是全部修改它们。

这就是我想要做的:我想要一种非常简单的方法来在父帖子页面上显示所有子帖子(按顺序)。

我在这里和那里找到了一些想法,但到目前为止似乎都没有。 (这里有一个例如: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 作为附加参数来选择具有某些元数据的帖子(如果我理解您是正确的,您现在将这个父子逻辑存储在元框中,是真的吗?)。除非您至少向我们展示一些代码/元框结构或自定义帖子结构,否则我们无法为您提供帮助
  • 实际上并没有试图让内容显示在元框中。我正在寻找的只是一种在父帖子页面上显示子帖子内容的简单方法。我可能已经提供了太多关于元框的信息,这使它有点混乱。我正在使用默认的帖子类型,但我已经对其添加了一些自定义,因此它可能不会被视为自定义帖子类型。我只想要父帖子上子帖子的“正常”内容。

标签: php wordpress


【解决方案1】:

好的,在循环中的帖子模板中尝试这样的操作。它应该可以帮助您在特定帖子中输出子帖子。 循环中的某处/

    $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 数组,这也应该可以。 循环中的某处/

    $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 函数获得的帖子内容的方法。这应该也可以工作

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

【讨论】:

  • 我尝试在循环中使用您的代码,但即使我知道这篇文章有一个孩子,它也没有返回任何内容。
  • 你在 single.php 中使用它吗?它通常用于输出单个帖子。你能告诉我你的代码吗?
  • 我已经编辑了原始帖子以显示我尝试过的代码。我很确定在这一点上这是愚蠢的,但我似乎无法让它发挥作用。谢谢你的帮助。
  • 伟大的最后一个为我做到了!谢谢
【解决方案2】:
/* 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" */

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2019-03-16
    • 1970-01-01
    • 2019-08-24
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多