So I have been searching online for something that I think is simple to achieve and I am most likely over thinking it. I have found many related articles on the matter but can't seem to get it to apply to my scenario. I have a sidebar menu on my site that displays a tier of sub-pages. So if you click on "Services" the sidebar shows

  • Service 1
  • Service 2
  • Service 3
  • Service 4
  • Service 5

This is perfect, as these are my 5 sub-pages underneath "Services". Now "Service 1, 2 & 3" have anywhere from 5-15 sub-pages under them. What I want to achieve is if you click on "Service 1" in the sidebar and the page loads the sidebar will adjust to show:

  • Service 1 -- sub-page 1 -- sub-page 2 -- sub-page 3
  • Service 2
  • Service 3
  • Service 4
  • Service 5

So on the 3rd tier of pages I still want the sidebar to display the main sub-services links but if you are on a sub-service page that has its own sub-pages I would like it to display those sub-pages as well as the original main service sub-pages.

Currently I have only been able to get this show like this:

Click on "Service 1", sidebar menu reflects: - Service 1 -- sub-page 1 -- sub-page 2 -- sub-page 3 - Service 2 - Service 3 - Service 4 - Service 5

Which is good, but then if I click on one of those "sub-pages" the menu updates to only show the sub-pages of that service and not the menu structure that was there before.

Sorry for the long post. I have tried this link using a custom walker: http://wordpress.mfields.org/2010/selective-page-hierarchy-for-wp_list_pages/ I have also tried:

        <ul class="sidebar_menu">
            <?php wp_list_pages( array('title_li'=>'','include'=>get_post_top_ancestor_id()) ); ?>
            <?php wp_list_pages( array('title_li'=>'','depth'=>1,'child_of'=>get_post_top_ancestor_id()) ); ?>

No luck though. Thanks for any help! EDIT: Also have tried the following (which works up until the 3rd tier is reached, then it drops the "2nd" tier of sub-pages.)

            $descendants = get_pages(array('child_of' => $ancestor_id));
            $incl = "";

            foreach ($descendants as $page) {
               if (($page->post_parent == $ancestor_id) ||
                   ($page->post_parent == $post->post_parent) ||
                   ($page->post_parent == $post->ID))
                  $incl .= $page->ID . ",";

            <?php wp_list_pages(array(
                "child_of" => $ancestor_id,
                "include" => $incl,
                "link_before" => "",
                "title_li" => "",
                "sort_column" => "menu_order"
  • You could global $post to retrieve the current page object. Then, just retrieve the parent or child pages of the current page object as necessary. – Michael Ecklund Dec 10 '13 at 17:11
  • @MichaelEcklund, thank you for the response. Based off of my edit above would you be able to assist me with this? I am not too familiar with the setup_postdata. Thank you! – Danny Dec 10 '13 at 17:27
  • I've provided a pretty vague answer, but I'll leave it up to you where you go from there. In my answer, you will have the current page VIA global $post;, all parent pages of the current page, along with all child pages of the current page. – Michael Ecklund Dec 10 '13 at 17:46

After you global $post, you can use the core WordPress function get_post_ancestors() to retrieve the parent pages.


$ancestors = get_post_ancestors($post);
    foreach(array_reverse($ancestors) as $post_id){
        $ancestor_page = get_post($post_id);

Then, to retrieve all child pages of the current page, you could make it easy by using a custom function. Place your custom function in your currently active theme functions.php file.


    function mbe_get_post_children($object){
        $data = array();
        $query = new WP_Query(array(
            'posts_per_page' => '-1',
            'post_type' => $object->post_type,
            'post_status' => 'publish',
            'post_parent' => $object->ID
            return false;
        foreach($query->posts as $child_post){
            $data[] = $child_post;
            if($child_post->post_parent != $object->ID && $child_post->post_parent != 0){
        return $data;

Then to use your custom function which retrieves all child pages of the current page, would be something very similar to the first example, which retrieves all parent pages of the current page.


    $children = mbe_get_post_children($post);
        foreach($children as $child_id){
            $child_page = get_post($child_id);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.