Get the top parent page in Wordpress
A couple of days ago I found myself with the issue of getting the top parent of any page (that one which doesn’t have any father), so after look in Wordpress documentation I found a nice function called get_page_hierarchy() and also no documentation about it. Without documentation about the function it was going to be a little bit tricky to know wheter or not this was what I was looking for, so I decided create my own function to receive the post_name of the top parent page. As soon I started I realized that my knowledges about recursivity were a little bit rusty, but I got it …
Here is the code:
<?php
//return the name of the top parent page base on a page_id
function getTopParentPostName($myid){
$mypage = get_page($myid);
if ($mypage->post_parent == 0){
return $mypage->post_name;
}
else{
return getTopParentPostName($mypage->post_parent);
}
}?>

5. November, 2008 at 22:12
I like people notice all the details and minutiae of everyday little thing could find something attractive and imperceptible to most. Super!