- This topic has 19 replies, 3 voices, and was last updated 4 years, 10 months ago by Tom.
-
AuthorPosts
-
May 1, 2019 at 7:25 am #885644Jonathan
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.
May 1, 2019 at 2:53 pm #885958TomLead DeveloperLead DeveloperHi 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.May 2, 2019 at 9:13 am #886854JonathanThanks. That worked well. Any chance of it being included in a future update?
May 2, 2019 at 11:13 am #886954TomLead DeveloperLead DeveloperI hope so! It’s something we’re looking at ๐
May 23, 2019 at 5:30 am #908653JonathanAny 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 HookMay 23, 2019 at 7:11 am #908858TomLead DeveloperLead DeveloperShould 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 );
May 23, 2019 at 7:35 am #908874JonathanIs the id the id of the hook?
May 23, 2019 at 7:38 am #908876TomLead DeveloperLead DeveloperYes, it’s the Element ID (appears in the address bar while you’re editing the Element).
May 23, 2019 at 9:02 am #908949JonathanHi,
Doesn’t seem to work on a hook element only the layout?
May 23, 2019 at 10:35 am #909023LeoStaffCustomer SupportHook element is a different filter:
https://docs.generatepress.com/article/generate_hook_element_display/June 7, 2019 at 1:38 am #922386JonathanI 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.
June 7, 2019 at 8:33 am #922762TomLead DeveloperLead DeveloperInstead of
generate_layout_element_hook
, trygenerate_hook_element_display
.Let me know ๐
June 8, 2019 at 3:56 am #923209JonathanWorks well, thanks.
June 8, 2019 at 3:58 am #923213JonathanOne last question. Could you add an exclusion to this rule i.e exclude the front page.
thanks again,
J
June 8, 2019 at 9:05 am #923469TomLead DeveloperLead DeveloperSure, for example:
if ( 456 === $element_id ) { if ( is_page_template( 'template1.php' ) ) { $display = true; } if ( is_front_page() ) { $display = false; } }
-
AuthorPosts
- You must be logged in to reply to this topic.