I have a page with id 63. That page is having a number of child pages and each of the child pages is again having sub pages. In short page with id 63 is super parent to all the pages.
What I want is, I want to check whether the current page is a child of page with id 63. Either a direct child/child of child up to any level.
I am able to check the whether the page is a child of page 63. But not able to check the same for child of child pages.
I am using $post->post_parent == 63
to check whether the page is a child of page 63.
How can I check the same for all levels of pages ?
$pageId= get_the_ID();
function get_topmost_parent($post_id)
{
$parent_id = get_post($post_id)->post_parent;
if($parent_id == 0)
{
return $post_id;
}
else
{
return get_topmost_parent($parent_id);
}
}
You can use get_post_ancestors() like this:
$parents = get_post_ancestors($post);
foreach($parents as $page_id){
if($page_id == ID of specific parent page){
// Do something
break; //Match found, no need to keep checking
}
}