最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

menus - List children of second level sub page

programmeradmin0浏览0评论

Top Level Page
- Second Level Page
--- Child Page
--- Child Page
--- Child Page

The goal here is to list all children of second level, I have that done from the codex. However, when I click on a Child, the children should still display because we're technically still inside of the Second Level.

<?php
// Globalize the $post variable;
// probably already available in this context, but just in case...
global $post;
    wp_list_pages( array(
        // Only pages that are children of the current page
        'child_of' => $post->ID,
        // Only show one level of hierarchy
        'depth' => 1
    ) );
?>

Top Level Page
- Second Level Page
--- Child Page
--- Child Page
--- Child Page

The goal here is to list all children of second level, I have that done from the codex. However, when I click on a Child, the children should still display because we're technically still inside of the Second Level.

<?php
// Globalize the $post variable;
// probably already available in this context, but just in case...
global $post;
    wp_list_pages( array(
        // Only pages that are children of the current page
        'child_of' => $post->ID,
        // Only show one level of hierarchy
        'depth' => 1
    ) );
?>
Share Improve this question asked Jan 30, 2015 at 17:48 edwardhimesedwardhimes 312 bronze badges 2
  • Do you always want to show all children to the top level page, even grandchildren? OR do you only want to show the grandchildren when you're view the second level page? – Howdy_McGee Commented Jan 30, 2015 at 17:57
  • I only want to show the grandchildren when im viewing the second level page.. – edwardhimes Commented Jan 30, 2015 at 18:03
Add a comment  | 

1 Answer 1

Reset to default 1

Is this what you are after?

<?php
    if($post->post_parent) {
        // if $post has parent than it is "Second level" and show its children.
        $children = wp_list_pages("child_of=".$post->post_parent."&echo=0");
    } else {
        // else it's a "Top level" so display children & grand children? 
        $children = wp_list_pages("child_of=".$post->ID."&echo=0");
    }

    if ($children) {
        echo "<ul>$children</ul>";
    }

Source: http://codex.wordpress/Function_Reference/wp_list_pages#List_subpages_even_if_on_a_subpage

发布评论

评论列表(0)

  1. 暂无评论