Checking for Grandparent Ancestor in WordPress

Consider this Page structure:

Page 1
|_ Page 1.1
|__ Page 1.1.1

View in question: A single Page

The requirement is to conditionally output an element only if the current Page's grandparent at the highest level in the hierarchy is the Page specified by its title, in this case - "Page 1".

In our example, the element should be rendered only if the current Page being viewed on the front end is either Page 1.1 or Page 1.1.1 and not on any other Page (incl. Page 1 itself).

This Pro tutorial shows how this can be done.

Note: This applies to any hierarchical post type, not just Pages.

Add the following in child theme's functions.php (w/o the opening PHP tag) or a code snippets plugin:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Instant access to 390+ Bricks code tutorials with BricksLabs Pro