Created
July 27, 2022 02:52
-
-
Save bobbydank/4943fb5bf6f25808a51f93259cd4afc6 to your computer and use it in GitHub Desktop.
Some random functions for getting the top page in a hierarchy.
This file contains 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
<?php | |
/** | |
* returns the top most ancestor post id for the current post | |
*/ | |
function catchylabs_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; | |
} | |
} | |
/** | |
* returns how many parents the current page has | |
*/ | |
function catchylabs_get_post_ancestors_count() { | |
global $post; | |
return count($post->ancestors); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment