WordPress – Show menu with page and subpages even if you are on subpage

Add on Function.php :

 
function get_top_parent_page_id() {

global $post;

$ancestors = $post->ancestors;

// Check if page is a child page (any level)
if ($ancestors) {

// Grab the ID of top-level page from the tree
return end($ancestors);

} else {

// Page is the top level, so use it’s own id
return $post->ID;

}

}

 

and in the template file :

<?php $parent_page = get_top_parent_page_id($post->ID); ?>

<ul>
<?php wp_list_pages(“child_of=$parent_page&title_li=”);
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>
</ul>

 

that’s all.