Support

Account

Home Forums General Issues Get field from ancestor page Reply To: Get field from ancestor page

  • The only thing I could see you adding is limiting it to the Grandparent at the most:

    
    if ( 0 == $post->post_parent ) {
        the_title();
    } else {
        $parents = get_post_ancestors( $post->ID );
        $parents = array_slice($parents, 0, 2);
        echo apply_filters( "the_title", get_the_title( end ( $parents ) ) );
    }
    

    I think that should give you either the parent or the grandparent, but not further back. If you want to limit it only to the grandparent then you’d need to make sure that the slice has a count of at least 2.