Show only its Sub Child pages in the wordpress sidebar of that Child page?

I have a Parent page(for expample – About-us) with multiple Child Pages (History, center, staff etc). Now few Child Pages also has its Sub Child Pages. So my left Sidebar of my Parent page looks like this.

About-us (parent)

Read More
  • History (child)
  • Center (child)
    • Center1 (sub child)
    • Center2 (sub child)
  • Staff (child)
    • staff_name1 (sub child)
    • staff_name2 (sub child)
    • staff_name3 (sub child)

When on the Parent page, I want to be able to display, in the left sidebar, only its Child Pages and NOT any of its Sub Child pages. So when on Parent (About-us) page, the sidebar should look like this :

  • History (child)
  • Center (child)
  • Staff (child)

And When on Child Page, it should display only its Sub Child Pages in the left sidebar and NOT any of the Parent’s Child pages.

So for example when you click on Center (child) page in the left sidebar of Parent (About-us) Page. It should display ONLY its Sub Child Pages of Center page

  • center1 (sub child)
  • center2 (sub child)

Here is my code. Please help.

<?php if(is_page()){
            $ancestor = get_post_ancestors($post);

       $page = $post->ID;

            if ($post->post_parent) {
                $page = $post->post_parent;
            }

            if($post->post_parent == 0){
                $children = wp_list_pages("sort_column=menu_order&title_li=&child_of=".$page."&echo=0");

            }else if($ancestor[1]){
                $children = wp_list_pages("sort_column=menu_order&title_li=&child_of=".$ancestor[1]."&echo=0");
            }

            else if($post->post_parent){
                $children = wp_list_pages("sort_column=menu_order&title_li=&child_of=".$page."&echo=0");
            }?>

            <?php if($children){ ?>
        <nav class="add-nav">
                   <ul>
                   <?php echo $children; ?>
                   </ul>
        </nav>
            <?php }
   }?> 

Related posts

Leave a Reply