Archived topic
Custom template not appearing in display rules
19 replies · Started by Scrat on August 29, 2018
Hi, I've made a custom template in order to target certain pages with the elements module. However, I can't seem to get the custom template to appear in the display rules. Steps to reproduce:
- Copy page.php from parent theme into child theme directory, renamed file to page-fullwidth.php, and added /* Template Name: Full Width Page */. So far so good - I have pages that are now Full Width Pages.
- Create a new header element Page Header Full Width.
- In Display Rules for the new header element, Location > Templates > [expected: Full Width Page; but it's not here].
Note: the problem I'm trying to solve is not to get custom templates to appear in the display rules per se; rather, I'd just like to have a way to target a group of pages, and templates seem to be a sensible way to do it. I'm open to other ideas.
Thank you!
Hi there,
So just to make sure, that group of pages you are trying to target have no existing relationships at all?
> So just to make sure, that group of pages you are trying to target have no existing relationships at all?
They all use the same template.
(They are also all subpages of a parent page, but there are other pages planned that will need to use the same header element that won't. I don't see a way to target subpages in the elements module either, so that point is probably moot.)
The idea is to set a header rule for pages that use the full-width layout. The client wants multiple pages that share the same header layout, each of which uses a different background image, heading, and tagline. The elements module makes it super easy to do so - I just can't see the custom template I created in the list of display rules.
I should also clarify - I'm trying to set a rule for pages that use a custom template that I called "Page Header Full Width", not the Generatepress native full-width page layout option. The problem is that I can't target custom templates in the element display rules. So if I made a template called Steve, for instance, I would expect to see Steve in Elements > [rule name] > Display Rules > Location > Templates > [here]. Hope that helps.
Hi there,
Display rules won't display page templates unfortunately - they only display post types and taxonomies.
However, we can use a filter if we have the ID of the element:
add_filter( 'generate_header_element_display', function( $display, $element_id ) {
if ( is_page_template( 'your-template.php' ) && 20 === $element_id ) {
return true;
}
return $display;
}, 10, 2 );
The above assumes the ID of the element is: 20
Hi Tom,
I also would like to assign a layout element to all pages that use a certain page template via display rules. But unfortunately I couldn't get the above mentioned filter to work in my local test installation:
I edited the template name and the ID of the layout element and copied the code to my child theme's functions.php. Did I miss something?
Do you plan to implement display rules for page templates in the future?
That would be so awesome (... and useful :))!
Thank you so much for your great work!
Best regards,
Sebastian
I'll definitely look into it, but I'm not sure how possible it will be.
What's the name of your template file?
The name of my template file is 'page-stack.php', the element ID is 442.
// GPP - LAYOUT ELEMENT FOR 'STACK' PAGE TEMPLATE
add_filter( 'generate_header_element_display', function( $display, $element_id ) {
if ( is_page_template( 'page-stack.php' ) && 442 === $element_id ) {
return true;
}
return $display;
}, 10, 2 );
Hmm, do you see a message below the header if you do this?:
add_action( 'generate_after_header', function() {
if ( is_page_template( 'page-stack.php' ) ) {
echo 'test';
}
} );
Yes I do. 'test' is sitting between the header and the content-wrapper.
Ok, so your conditions look fine.
Just to confirm, another header element isn't already assigned to that page?
No. I created one layout element (no sidebars, no title, full-width) and assigned it to my front-page via display rules. My front-page uses the above mentioned page-stack.php template. Now I'm looking for a way to apply the layout element to all pages that use the page-stack.php template. The code above is in place (functions.php). But if I assign the page-template to a new page, this page still has sidebar, title and boxed content.
Right now only one Layout can be active on a specific page. We're changing this in GPP 1.8.
So if a Layout element is already active due to it being the front page, the other Layout elements won't apply even if it's set to the page template.
Okay. So for the moment, the only way to apply the layout element to all pages using my custom page template is to set the display rules to ALL pages and than exclude the pages that don't use the custom page template?
That works for me. The ability to set the display rules by page template would be awesome though.
Already looking forward to 1.8 ;)
Thanks again for your work and great support!
That's correct :)
No problem!