Wordpress Tips

WordPress Tips: Find the ID of the Topmost Parent Page

In the event that your page structure has different levels, the accompanying code will discover the parent page ID of the highest level of a page.

<?php
if ($post->post_parent)	{
	$ancestors	= get_post_ancestors($post->ID);
	$root		= count($ancestors)-1;
	$parent		= $ancestors[$root];
} else {
	$parent		= $post->ID;
}
?>

Leave a Reply