Conditional statement for parent, child and grandchild pages

Use get_post_ancestors( $post ). It returns an array of post IDs. The parent ID is the first entry, the grand parent the second and so on.

$ancestors = get_post_ancestors( $post );

if ( is_page( 'about' ) || in_array( '29', $ancestors ) )
{
    echo "This is About or one of its descendants";
}
elseif ( is_page( 'contact' ) || in_array( '31', $ancestors ) )
{
    echo "This is Contact or one of its descendants";
}

Leave a Comment