Archived topic
Display Rule for a specific page template
19 replies · Started by Jonathan on May 1, 2019
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.
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.
Thanks. That worked well. Any chance of it being included in a future update?
I hope so! It's something we're looking at :)
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
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 );
Is the id the id of the hook?
Yes, it's the Element ID (appears in the address bar while you're editing the Element).
Hi,
Doesn't seem to work on a hook element only the layout?
Hook element is a different filter:
https://docs.generatepress.com/article/generate_hook_element_display/
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.
Instead of generate_layout_element_hook, try generate_hook_element_display.
Let me know :)
Works well, thanks.
One last question. Could you add an exclusion to this rule i.e exclude the front page.
thanks again,
J
Sure, for example:
if ( 456 === $element_id ) {
if ( is_page_template( 'template1.php' ) ) {
$display = true;
}
if ( is_front_page() ) {
$display = false;
}
}
