P
P
pavel_samael2021-09-29 09:59:38
WordPress
pavel_samael, 2021-09-29 09:59:38

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

1 answer(s)
A
Artem Zolin, 2021-09-29
@artzolin

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 );
  }
}

Use in a loopget_top_ancestor();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question