生成显示顶级父级的所有子页面的菜单

时间:2018-03-28 作者:Steve

我用过this answer\'s 生成显示父页面所有子页面的小部件的代码:

if (is_page()) {
  global $wp_query;

  if( empty($wp_query->post->post_parent) ) {
    $parent = $wp_query->post->ID;
  } else {
    $parent = $wp_query->post->post_parent;
  }

  if(wp_list_pages("title_li=&child_of=$parent&echo=0" )) {
    wp_list_pages("title_li=&child_of=$parent&echo=1" );
  }
}
我现在意识到,如果我在孙子页面上,这是不可能的,因为叔叔和阿姨不会显示,只有父母。

我不明白如何调整此代码以显示两级以上的菜单树。

感谢您的帮助。

3 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

这是一个将输出顶级父级以及当前页面的所有子级的函数。将有用的菜单类添加到输出中。E、 g。page_item_has_children, current_page_item, current_page_ancestor, 等

此解决方案是based on one of the examples 在文档中wp_list_pages()this answer 这里是WPSE,它引用了相同的示例。

/**
 * Use wp_list_pages() to display parent and all child pages of current page.
 */
function wpse_get_ancestor_tree() {
    // Bail if this is not a page.
    if ( ! is_page() ) {
      return false;
    }

    // Get the current post.
    $post = get_post();

    /**
     * Get array of post ancestor IDs.
     * Note: The direct parent is returned as the first value in the array.
     * The highest level ancestor is returned as the last value in the array.
     * See https://codex.wordpress.org/Function_Reference/get_post_ancestors
     */
    $ancestors = get_post_ancestors( $post->ID );

    // If there are ancestors, get the top level parent.
    // Otherwise use the current post\'s ID.
    $parent = ( ! empty( $ancestors ) ) ? array_pop( $ancestors ) : $post->ID;

    // Get all pages that are a child of $parent.
    $pages = get_pages( [
                     \'child_of\' => $parent,
                     ] );

    // Bail if there are no results.
    if ( ! $pages ) {
        return false;
    }

    // Store array of page IDs to include latere on.
    $page_ids = array();
    foreach ( $pages as $page ) {
        $page_ids[] = $page->ID;
    }

    // Add parent page to beginning of $page_ids array.
    array_unshift( $page_ids, $parent );

    // Get the output and return results if they exist.
    $output = wp_list_pages( [
        \'include\'  => $page_ids,
        \'title_li\' => false,
        \'echo\'     => false,
    ] );

    if ( ! $output ) {
        return false;
    } else { 
        return \'<ul class="page-menu ancestor-tree">\' . PHP_EOL .
                            $output . PHP_EOL .
                        \'</ul>\' . PHP_EOL;
    }
}

Usage:

echo wpse_get_ancestor_tree();

Example Page Structure:

Parent Page
  Child Page 01
  Child Page 02
  Child Page 03
    Grandchild Page
      Great Grandchild Page
  Child Page 04
  Child Page 05

Example Output (current page: Great Grandchild Page)

<ul class="page-menu ancestor-tree">
    <li class="page_item page-item-1088 page_item_has_children current_page_ancestor">
        <a href="http://example.com/parent-page/">Parent Page</a>
        <ul class="children">
            <li class="page_item page-item-1090">
                <a href="http://example.com/parent-page/child-page-01/">Child Page 01</a>
            </li>
            <li class="page_item page-item-1092">
                <a href="http://example.com/parent-page/child-page-02/">Child Page 02</a>
            </li>
            <li class="page_item page-item-1094 page_item_has_children current_page_ancestor">
                <a href="http://example.com/parent-page/child-page-03/">Child Page 03</a>
                <ul class="children">
                    <li class="page_item page-item-1102 page_item_has_children current_page_ancestor current_page_parent">
                        <a href="http://example.com/parent-page/child-page-03/grandchild-page/">Grandchild Page</a>
                        <ul class="children">
                            <li class="page_item page-item-3066 current_page_item">
                                <a href="http://example.com/parent-page/child-page-03/grandchild-page/great-grandchild-page/">Great Grandchild Page</a>
                            </li>
                        </ul>
                </li>
            </ul>
        </li>
        <li class="page_item page-item-1096">
            <a href="http://example.com/parent-page/child-page-04/">Child Page 04</a>
        </li>
        <li class="page_item page-item-1098">
            <a href="http://example.com/parent-page/child-page-05/">Child Page 05</a>
        </li>
    </ul>
    </li>
</ul>

SO网友:Alex Davison

我相信您目前只是在获取父级,而没有检查顶级祖先。

尝试替换:

  if( empty($wp_query->post->post_parent) ) {
    $parent = $wp_query->post->ID;
  } else {
    $parent = $wp_query->post->post_parent;
  }
使用:

if ($post->post_parent) {
    $ancestors=get_post_ancestors($post->ID);
    $root=count($ancestors)-1;
    $parent = $ancestors[$root];
} else {
    $parent = $post->ID;
}
依据:https://css-tricks.com/snippets/wordpress/find-id-of-top-most-parent-page/

SO网友:T.Todua

正确代码:

if( empty($wp_query->post->post_parent) ) {
    $parent = $wp_query->post->ID;
} else {
    $ancestors=get_post_ancestors($wp_query->post->ID);
    $parent = $ancestors[count($ancestors)-1];
}
它将访问当前页面的最顶层页面。

结束