List Child Pages of Parent Parent Page (Child pages from Grand Parent)

OK I’m doing a a menu (sidebar menu) that will display this all child pages of parent page (currently open)
Parent
|-Child 1
|-Child 2
|Child 3

but in same time when someone is in bolded page (child) will see the same thing
Parent
|-Child 1
_
_|-Child 1
_ _|-Child 2
_ _|- Child 3
|-Child 2
|-Child 3

currently I use next code which do first job well but not the second one with child of child I tried several things but nothing worked

 <?php if ( is_page() ) { 
if($post->post_parent)
$children = wp_list_pages('title_li=&child_of=".$post->post_parent."&echo=0&sort_column=post_date&sort_order=DESC'); else
$children = wp_list_pages('title_li=&child_of=".$post->ID."&echo=0&sort_column=post_date&sort_order=DESC');
if ($children) { ?> .......

Thanks in advance


Edit

As Simon answered it worked but not for all cases and I edited so now final code is:

<?php if ( is_page() ) {
    $stats = count($post->ancestors);
if($post->post_parent){ 
  if($stats == 2){
    $children = wp_list_pages('title_li=&child_of=".get_post( $post->post_parent )->post_parent."&echo=0&sort_column=post_date&sort_order=DESC&depth=1'); 
  }else{
    $children = wp_list_pages('title_li=&child_of=".$post->post_parent."&echo=0&sort_column=post_date&sort_order=DESC&depth=1');
  }}
else{
  $children = wp_list_pages('title_li=&child_of=".$post->ID."&echo=0&sort_column=post_date&sort_order=DESC&depth=1');
}
if ($children) { ?> ....

That’s in case if somebody else need this stuff, Cheers and thanks for fast help

2 Answers
2

Could you try this ? The idea is if there is a parent, we should list pages for the parent of this parent.

<?php if ( is_page() ) { 

    if($post->post_parent) {
        $children = wp_list_pages('title_li=&child_of=". get_post( $post->post_parent )->post_parent ."&echo=0&sort_column=post_date&sort_order=DESC'); 
    } else {
        $children = wp_list_pages('title_li=&child_of=".$post->ID."&echo=0&sort_column=post_date&sort_order=DESC');
    }
}

Other solution would be to try to use depth option which according to codex could list only top-level pages :

<?php if ( is_page() ) { 

    if( $post->post_parent ) {
        $children = wp_list_pages('title_li=&depth=1&echo=0&sort_column=post_date&sort_order=DESC'); 
    } else {
        $children = wp_list_pages('title_li=&child_of=".$post->ID."&echo=0&sort_column=post_date&sort_order=DESC');
    }
}

Sorry, I don’t have time to try the code, but let us know if this is helps (or not).

Leave a Comment