Find ID of Top-Most Parent Page

Avatar of Chris Coyier
Chris Coyier on

This will find what the ID is of the top-most parent Page, in a nested child page. For example, this page you are literally looking at is nested under


if ($post->post_parent)	{
	$parent = $ancestors[$root];
} else {
	$parent = $post->ID;


$parent will be the correct ID. For example, for use with wp_list_pages.

Source: CSSGlobe