April 24, 2019
I used this recently coupled with breadcrumbs to prevent certain items from showing on the top-most items in page hierarchy.
function get_top_parent_page_id() {
global $post;
// Check if page is a child page (any level)
if ($post->ancestors) {
// Grab the ID of top-level page from the tree
return end($post->ancestors);
} else {
// Page is the top level, so use it’s own id
return $post->ID;
// return 1;
}
}
To check for the top-most page, I added the following condition to my the body:
<?php if(get_the_ID() == get_top_parent_page_id()) { echo 'topmost-page'; } ?>