treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Find ID of Top-Most Parent Page

Last updated 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

<?php

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

?>

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

Source: CSSGlobe

View Comments

Comments

  1. samavia
    Permalink to comment#

    Really nice .. it works… it save me from frustration..thanks :)

  2. Scott
    Permalink to comment#

    This is just what I needed for what I was working on today. Thanks!

Leave a Comment

Use markdown or basic HTML and be nice.