Skip to content

Instantly share code, notes, and snippets.

@bobbydank
Created July 27, 2022 02:52
Show Gist options
  • Save bobbydank/4943fb5bf6f25808a51f93259cd4afc6 to your computer and use it in GitHub Desktop.
Save bobbydank/4943fb5bf6f25808a51f93259cd4afc6 to your computer and use it in GitHub Desktop.
Some random functions for getting the top page in a hierarchy.
<?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