I am using a page hierarchy and I want to show the title of the 2nd parents The structure is something like

Main Page

Main Page > Second page

Main Page > Second page > Third page

Main Page > Second page > Third page > Fourth page

I want to show the Second page title in the Fouth page.

How can i do this? Any help would be appreciated....

2 Answers 2


get_post_ancestors() returns all of the ancestors hierarchically. As you only want to display the 2nd level post title on 4th post page, you can do something like -

global $post;

$parents = get_post_ancestors( get_the_ID() );

if( count($parents) == 4 ){

    // ancestor1 = 1st parent, ancestor2 = second.
    echo get_the_title($parents['1']); // index starts from 0, 1 = 2nd item

The function you are looking for is get_post_ancestors()

According to the documentation it returns an array of the ancestor page IDs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.