Cyber Week Sale! Get $20 off GP Premium, $40 off our new Lifetime license, and 45% off license renewals/extensions! Learn more

[Resolved] Display Rule for a specific page template

Home Forums Support Display Rule for a specific page template

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #885644
    Jonathan

    Is it possible with the new layout element to have a display rule for a specific page template? I’ve create a custom page template (placed within my-templates folder called jjma.php) for a section of a web site that I would like a specific layout to be displayed. At the moment I can’t see the page template in the display rules under the template drop down.

    #885958
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Page Templates don’t exist in the Display Rules at this time.

    However, we can use a filter to tell a specific element to display.

    For example:

    add_filter( 'generate_layout_element_display', function( $display, $element_id ) {
        if ( 123 === $element_id ) {
            if ( is_page_template( 'templates/jjma.php' ) ) {
                $display = true;
            }
        }
    
        return $display;
    }, 10, 2 );

    You just need to update 123 to the ID of the element.

    #886854
    Jonathan

    Thanks. That worked well. Any chance of it being included in a future update?

    #886954
    Tom
    Lead Developer
    Lead Developer

    I hope so! It’s something we’re looking at 🙂

    #908653
    Jonathan

    Any chance of adapting the code for a hook to display for the page template selected? I have four sections and I want the element hook to display depending on page template selected.

    template1.php — > One Element Hook
    template2.php — > Next Element Hook
    template3.php — > Next Element Hook
    template4.php — > Next Element Hook

    #908858
    Tom
    Lead Developer
    Lead Developer

    Should be something like this:

    add_filter( 'generate_layout_element_display', function( $display, $element_id ) {
        if ( 123 === $element_id ) {
            if ( is_page_template( 'templates/jjma.php' ) ) {
                $display = true;
            }
        }
    
        if ( 456 === $element_id ) {
            if ( is_page_template( 'template1.php' ) ) {
                $display = true;
            }
        }
    
        if ( 789 === $element_id ) {
            if ( is_page_template( 'template2.php' ) ) {
                $display = true;
            }
        }
    
        // etc..
    
        return $display;
    }, 10, 2 );
    #908874
    Jonathan

    Is the id the id of the hook?

    #908876
    Tom
    Lead Developer
    Lead Developer

    Yes, it’s the Element ID (appears in the address bar while you’re editing the Element).

    #908949
    Jonathan

    Hi,

    Doesn’t seem to work on a hook element only the layout?

    #909023
    Leo
    Staff
    Customer Support
    #922386
    Jonathan

    I tried changing the code to use the ‘generate_layout_element_hook’ so that if a page had a template assigned to it then it the hook would display. Unfortunately it didn’t work:

    //GPRESS ——–

    // Element_id is the element created in display rules

    add_filter( 'generate_layout_element_hook', function( $display, $element_id ) {
        if ( 60191 === $element_id ) {
            if ( is_page_template( 'my-templates/about.php' ) ) {
                $display = true;
            }
        }
    
        if ( 456 === $element_id ) {
            if ( is_page_template( 'template1.php' ) ) {
                $display = true;
            }
        }
    
        if ( 789 === $element_id ) {
            if ( is_page_template( 'template2.php' ) ) {
                $display = true;
            }
        }
    
        // etc..
    
        return $display;
    }, 10, 2 );

    The template is shown but not the hook associated with the template.

    #922762
    Tom
    Lead Developer
    Lead Developer

    Instead of generate_layout_element_hook, try generate_hook_element_display.

    Let me know 🙂

    #923209
    Jonathan

    Works well, thanks.

    #923213
    Jonathan

    One last question. Could you add an exclusion to this rule i.e exclude the front page.

    thanks again,

    J

    #923469
    Tom
    Lead Developer
    Lead Developer

    Sure, for example:

    if ( 456 === $element_id ) {
        if ( is_page_template( 'template1.php' ) ) {
            $display = true;
        }
    
        if ( is_front_page() ) {
            $display = false;
        }
    }
Viewing 15 posts - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.