Created
November 21, 2014 09:45
-
-
Save yanknudtskov/db7b81aab3498fbef41a to your computer and use it in GitHub Desktop.
Get topmost parent of WordPress Page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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