Test if page is child and has children, if so echo child pages also on grandchild pages

Use ancestors:

$ancestors = array_reverse( get_post_ancestors( $post->ID ) ); // reverse ancestors to make it more intuitive

if ( isset( $ancestors[0] ) ) {

    if ( isset( $ancestors[1] ) ) {
        // 3rd tier
        $parent_id = $post->post_parent;
    } else {
        // 2nd tier
        $parent_id = $post->ID;
    }

    $args = array(
        'depth' => 1,
        'child_of' => $parent_id,
    );

    wp_list_pages( $args );
}

Leave a Comment