I need a conditional statement which checks to see if the page the user is currently on is a child of a specific parent page or that parent page itself, and if so, display a link. How do I achieve this in WP?

1 Answer 1


Use get_post_ancestors():

    // get page ancestors
    $parents = get_post_ancestors( $post->ID );

    // id of parent page you want to find
    $parent_id = 123;

    // check of $parent_id
    if ( $post->ID == $parent_id || in_array( $parent_id, $parents ) ) {
        echo '<a href="#">Link</a>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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