Skip to content

Instantly share code, notes, and snippets.

@yanknudtskov
Created November 21, 2014 09:45
Show Gist options
  • Select an option

  • Save yanknudtskov/db7b81aab3498fbef41a to your computer and use it in GitHub Desktop.

Select an option

Save yanknudtskov/db7b81aab3498fbef41a to your computer and use it in GitHub Desktop.
Get topmost parent of WordPress Page
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;
}
}
<ul>
<?php wp_list_pages("child_of=$parent_page&title_li="); ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment