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
Really nice .. it works… it save me from frustration..thanks :)
This is just what I needed for what I was working on today. Thanks!
Perfect! Thanks .. i need it today for my work, it safe my time :)
Use this one $post->post_parent
Perfect! This was a great time-saver. If you’d like to place this in your functions.php file for repeated use, you could wrap it in a function like this:
Thank you, very helpful!
I have a slightly more succinct version
Thank you Jean-Pierre, this worked perfectly! I’m always looking for the most succinct version.
It Works! Smilling…
Im getting an error with this code, hoping to get some help
PHP Notice: Undefined offset:
it is in relation to this line: $parent = $ancestors[$root];
Any help?