You can use wp_list_pages
with the child_of
argument:
/**
* Display hierarchical list of all posts for a given post.
*
* @param int|WP_Post $post Display for post. Defaults to current post/page.
*/
function wpse_184554_list_section( $post = null ) {
if ( ! $post = get_post( $post ) )
return;
if ( $post->post_parent && $ancestors = get_post_ancestors( $post ) )
$parent_id = array_pop( $ancestors );
else
$parent_id = $post->ID;
echo '<ul class="pages">';
wp_list_pages(
array(
'post_type' => $post->post_type, // Will work for any hierarchical post type
'child_of' => $parent_id,
'title_li' => '',
)
);
echo '</ul>';
}
This will output a hierarchical list of all pages that are children of the current page’s top level parent (or simply all child pages if the current page has no parent).
Pop it in your functions.php
. To use, call it in any template file, for example:
<div class="sidebar">
<?php wpse_184554_list_section() ?>
</div>