display rule for pages – parent + sub pages

  • Hi, i created a content layout and i want it to assign to a page (parent) and it’s sub-pages (child), how can i do that without selecting them manually?

  • Hi,

    There’s no native “child pages of X” condition in GP Elements Display Rules — you’d need to either add each child page manually (which doesn’t scale) or use a small PHP snippet to extend the logic.

    Add this via a Code Snippets plugin or your child theme’s functions.php:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if ( 123 === $element_id && is_singular( 'page' ) ) {
            $ancestors = get_post_ancestors( get_the_ID() );
            if ( in_array( 456, $ancestors, true ) ) {
                $display = true;
            }
        }
        return $display;
    }, 10, 2 );

    Replace 123 with your Content Layout Element’s ID and 456 with the parent page’s ID. Keep the parent page itself in the Element’s Display Rules as normal — the snippet only adds coverage for its descendants.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.