Get parent page ID in WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 | 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; } } |