【问题标题】:How to get all child pages of a parent page in wordpress?如何在wordpress中获取父页面的所有子页面?
【发布时间】:2013-05-10 18:15:22
【问题描述】:

例子:

About
--- Menu 1
--- Menu 2
--- Menu 3
--- Menu 4

如果我在关于页面...我有子页面。 但是,如果进入菜单 1,所有页​​面都会消失

我需要的是一直查看父页面

目前我有这个代码

<? if (is_page()) {
    $g_page_id = $wp_query->get_queried_object_id();
    wp_list_pages("depth=4&title_li=&child_of=".$g_page_id."&sort_column=menu_order");
   }
?>

谢谢!

已解决

我使用它并且工作正常!

<?php
if ( is_page() ) :
    if( $post->post_parent ) :
        $children = wp_list_pages( 'title_li=&child_of='.$post->post_parent.'&echo=0' );
    else;
        $children = wp_list_pages( 'title_li=&child_of='.$post->ID.'&echo=0' );
    endif;
    if ($children) : ?>
        <div class="title">
            <?php
            $parent_title = get_the_title( $post->post_parent );
            echo $parent_title;
            ?>
            <span></span>
        </div>
        <ul>
            <?php echo $children; ?>
        </ul>
    <?php
    endif;
endif;
?>

【问题讨论】:

标签: php wordpress parent-child


【解决方案1】:

给你。作者有点晚了,但人们仍然会来这里寻求答案;-)

<?php 
// determine parent of current page
if ($post->post_parent) {
    $ancestors = get_post_ancestors($post->ID);
    $parent = $ancestors[count($ancestors) - 1];
} else {
    $parent = $post->ID;
}

$children = wp_list_pages("title_li=&child_of=" . $parent . "&echo=0");

if ($children) {
?>

    <ul class="subnav">
        <?php 
            // current child will have class 'current_page_item'
            echo $children; 
        ?>
    </ul>

<?php 
} 
?>

【讨论】:

  • 您已经在$post-&gt;post_parent 中有父ID,那么为什么要使用get_post_ancestors 来获取父ID?
【解决方案2】:

处理此问题的最简单方法是使用get_children()。它几乎可以满足您的期望。它返回父页面的子页面。

get_children() 基本上是WP_Query 类的包装器。

你可以这样使用它...

$child_args = array(
    'post_parent' => $post->ID, // The parent id.
    'post_type'   => 'page',
    'post_status' => 'publish'
);

$children = get_children( $child_args );

如果要返回当前帖子的子项,可以将$post-&gt;ID 传递为'post_parent'。否则,请使用您想要的任何 ID。

Documentation for get_children()

Documentation for WP_Query

【讨论】:

    【解决方案3】:
    /* 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" */
    
    /* 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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-17
      • 2018-05-11
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多