I've created a recursive function to find out if current page is a child of page in a page tree.
My function:
function is_ancestor_of( $page_name = null, $post ) {
if(is_null($page_name))
return false;
// does it have a parent?
if (!isset( $post->post_parent ) OR $post->post_parent <= 0 )
return false;
//Get parent page
$parent = wp_get_single_post($post->post_parent);
if ( $parent->post_name == $page_name ){
echo $parent->post_name.' - '.$page_name;
return true;
} else {
is_ancestor_of( $page_name, $parent );
}
}
This is the code I'm testing with:
$is_parent = is_ancestor_of( 'account', $post );
if(!$is_parent) {
echo '<br/> No match';
}
And this is the result:
account - account
No match
This shows that it does find a parent page names 'account', but it is not returning any data. I've also tested returning a string, but that is not working either.
Can anyone see what I'm doing wrong?