Answer the question
In order to leave comments, you need to log in
How to get the link and title of parent parent page?
I have a bunch of pages, they have a parent page, and the parent page also has a parent page, and this is how to get its id? Just inserting an id will not work, you need it to automatically output, since this is a template.
Answer the question
In order to leave comments, you need to log in
Use this recursive function
function get_top_ancestor( $id = get_the_ID() ) {
$current = get_post( $id );
if( $current->post_parent === 0 ) {
return $current->ID;
} else {
return get_top_ancestor( $current->post_parent );
}
}
get_top_ancestor();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question