0

I'm looking to show a certain block of content if the current page is a grandchild of a top level page.

This is my current page heirarchy;

- Top Level Page
    - Child Page
        - Grandchild Page
        - Grandchild Page
        - Grandchild Page
    - Child Page
        - Grandchild Page
        - Grandchild Page
        - Grandchild Page
    - Child Page
        - Grandchild Page
        - Grandchild Page
        - Grandchild Page

So the top level page has 3 children, each of which have 3 children of their own. How can I target ONLY the grandchildren?

1 Answer 1

1

Check how many ancestors the page has via get_post_ancestors:

// grandchild pages will have two or more ancestors
if( 2 <= count( get_post_ancestors( $post->ID ) ) ){
    echo 'grandchild page';
}

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.