How to show only parents subpages of current page item in vertical menu?

The question:

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 3.2.2.2:

Page 1 Page 2 Page 3 Page 4

  • Page 3.1
  • Page 3.2
    • Page 3.2.1
    • Page 3.2.2
      • Page 3.2.2.1
      • Page 3.2.2.2
        • Page 3.2.2.2.1
        • Page 3.2.2.2.2
      • Page 3.2.2.3
    • Page 3.2.3
  • Page 3.3
  • Page 3.4
  • Page 3.5

So, only submenus for Page 3.2.2.2‘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.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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(
    array(
      '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).'" rel="nofollow noreferrer noopener">';
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:

<?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);
?>

Method 2

Try using this snippet from WordPress Codex:

http://codex.wordpress.org/Template_Tags/wp_list_pages#List_current_Page.2C_ancestors_of_current_page.2C_and_children_of_current_page


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment