0

Firstly, apologies for the terminology used here, I will try to be as descriptive as possible.

I am trying to create a menu system that will achieve the following:

  1. List top level pages as one menu (level 1)
  2. When on a top level page, list the child pages in a separate menu (level 2)
  3. When on a child page, list the child pages of that child as a further separate menu (level 3)

From the Codex this creates the menu which happily lists 3 levels of pages:

<?php if(!$post->post_parent){
    // will display the subpages of this top level page
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
   }

   else{
    // diplays only the subpages of parent level
    //$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");

    if($post->ancestors)
    {
     // now you can get the the top ID of this page
     // wp is putting the ids DESC, thats why the top level ID is the last one
     $ancestors = end($post->ancestors);
     $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
     // you will always get the whole subpages list
    }
   }

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

This is fine, but all page listings are in a single list, in one place.

I need to split this out so I can just grab the child pages, and display that menu in a <div> in one part of my page, then i need to grab the third level pages, and display those in another part of my page, in a separate <div>.

Any clues how to do this?

Many thanks

3 Answers 3

1

To list top level pages only:

wp_list_pages( 'depth=1' );

To list children of the current page, but only if it is a top level page:

if ( 0 == $post->post_parent) {
    wp_list_pages( 'child_of=' . $post->ID . '&depth=1' );
}

To list children of the current page, but only if it is a child page:

if ( 0 != $post->post_parent) {
    wp_list_pages( 'child_of=' . $post->ID . '&depth=1' );
}
1

Here is the code to post the grandchildren only:

<?php 
if ( 0 != $post->post_parent) {
    // will display the subpages of this top level page
    $children = wp_list_pages( 'child_of=' . $post->ID . '&depth=1' );
}

else{
    // diplays only the subpages of parent level
    //$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");

    if($post->ancestors)
    {
        // now you can get the the top ID of this page
        // wp is putting the ids DESC, thats why the top level ID is the last one
        $ancestors = end($post->ancestors);
        $children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
        // you will always get the whole subpages list
    }
}

if ($children) { ?>
<ul>
    <?php echo $children; ?>
</ul>
<?php } ?>
0

You're using wp_list_pages to check if your page has children. Try using get_children instead (syntax here).

wp_list_pages echoes its output, which is not the behaviour you want.

2
  • Thanks, I am reading up on get_children now. Do you think what I am trying to achieve is actually possible?
    – Dave
    Dec 3, 2010 at 19:29
  • Wait, sorry. I didn't see that you were using echo=0 in your original check for children. What you're doing should be essentially the same as what I suggested. I would still use get_children instead, but thats just personal preference. Guess I didn't understand your question correctly. Does it work for you to include depth=1 in the args for wp_list_pages, so that you only get immediate children and not the whole page tree? Dec 3, 2010 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.