I just can't find a way to make it possible to show only current parent items down to current page item in a vertical menu the "wordpress way".

What I want to achieve is the following dynamic structure, if I visit Page

Page 1 Page 2 Page 3 Page 4

  • Page 3.1
  • Page 3.2
    • Page 3.2.1
    • Page 3.2.2
      • Page
      • Page
        • Page
        • Page
      • Page
    • Page 3.2.3
  • Page 3.3
  • Page 3.4
  • Page 3.5

So, only submenus for Page's parents and it's own (if it has a submenu) will be listed, even if Page 3.4 or Page 3.2.3 also has submenus.

  • I really want to thank you for this solution. Do you know if there is any new method of doing this, or are you still using this approach? Thanks again. - Jay
    – user35703
    Commented Jul 24, 2013 at 5:15

2 Answers 2


Finally, I solved it myself. Here is the solution:

In functions.php:

function show_all_children($parent_id, $post_id, $current_level)
$top_parents    = array();
$top_parents    = get_post_ancestors($post_id);
$top_parents[]  = $post_id;

$children = get_posts(
      'post_type'       => 'page'
    , 'posts_per_page'  => -1
    , 'post_parent'     => $parent_id
    , 'order_by'        => 'title'
    , 'order'           => 'ASC'

if (empty($children)) return;

echo '<ul class="children level-'.$current_level.'-children">';

foreach ($children as $child)
echo '<li';
    if (in_array($child->ID, $top_parents))
    echo ' class="current_page_item"';
echo '>';

echo '<a href="'.get_permalink($child->ID).'">';
echo apply_filters('the_title', $child->post_title);
echo '</a>';

    // now call the same function for child of this child
    if ($child->ID && (in_array($child->ID, $top_parents)))
    show_all_children($child->ID, $post_id, $current_level+1);

echo '</li>';

echo '</ul>';

In sidebar.php:

$parents_ids   = get_post_ancestors($post->ID);
$top_parent_id = (count($parents_ids) > 0) ? $parents_ids[count($parents_ids)-1] : $post->ID;
show_all_children($top_parent_id, $post->ID, 1);
  • this is fantastic! i've been searching for a solution that does this for ages! this works perfectly, and i can't thank you enough!
    – user17534
    Commented Jun 26, 2012 at 14:15

Try using this snippet from WordPress Codex:


  • That code doesn't keep the parent's sibling pages. I want the whole menu list for each parent of the current page to be listed. But not the cousin's menus. Commented Sep 14, 2011 at 17:33
  • Seriously. Isn't this type of menu, among the most common? What a miss by Wordpress not made ​​this possible in a simple manner. So, that is, no one has a solution to this? Commented Sep 15, 2011 at 8:09
  • Then, iterate on the root pages. Find the current page's first ancestor (in this case it would be Page 3.2) and build an array with all the post->parent IDs, working backwards. It is more difficult to explain than actually do it. Hope it helps.
    – mines
    Commented Sep 15, 2011 at 10:30

Your Answer

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

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