Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Display rules only for subpages

  • I’m using this code I found on the forum:

    add_filter( 'generate_header_element_display', function( $display, $element_id ) {
        global $post;
    
        if ( 23059 === $element_id && ( is_page() && 8418 == $post->post_parent ) ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );

    I’m actually using two. One to show a content template for subpages of page 8418, and one for the page layout (= no sidebar, no title).

    Within the element I tried to set the general display rule to all pages, whole site, or the page title of the parent page (8418).

    In all cased the blocks I added in the content template element show on ALL pages of the website, not just the subpages of that parent page.

    Am I missing something?

    PS I changed generate_hook_element_display into generate_header_element_display after a suggestion by Tom on another topic, but in my case that makes no difference.

  • I found this topic

    https://generatepress.com/forums/topic/gp-hook-on-certain-child-pages/#post-1056464

    but that doesn’t work either in my case. All pages loose their design, as if no content layout is present anymore on any page.

  • Thank you AI…

    This is the corrected code, and with this it is okay to leave the display rule in the element to ‘Entire site’.

    add_filter( ‘generate_element_display’, function( $display, $element_id ) {
    global $post;

    // Replace 23059 with your Content Template Element ID
    if ( 23059 === $element_id ) {
    if ( is_page() && $post->post_parent == 8418 ) {
    return true; // show element
    } else {
    return false; // hide element
    }
    }

    return $display;
    }, 10, 2 );

    This sub pages thing added to the display rule options in an element would be nice 😉

  • My fault was not knowing about the differences of the filters.

    AI:

    You’re using the filter generate_header_element_display, but your element type is a Content Template, not a Header. Each element type has its own display filter in GeneratePress Premium:

    Header Element → generate_header_element_display
    Hook Element → generate_hook_element_display
    Layout Element → generate_layout_element_display
    Block Elements (like Content Templates) → generate_element_display

    So in your case, you need to switch to generate_element_display.

  • Glad you’ve got this sorted 🙂

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