List all subpages hierarchically based on the currently viewed page, top ancestor levels included

You can use get_post_ancestors() (codex) to get an array of all the ancestors id’s, so if I understand you correctly you might want to use


instead of


where end() (php doc) gives you the last element of the array.