Archived topic
Add header Element to certain Page template
5 replies · Started by Dave on March 29, 2021
Hey Guys,
I am looking at applying a header element to a custom page template.
Is this possible?
The reason is I want to target certain pages that are using only a certain page template.
any help would be appreciated.
cheers,
Dave
Hi there,
Have you tried the Header element's Display Rule location?
I suggest creating a specific taxonomy for pages using CPT UI plugin.
Example:
1.) Create custom_page_template taxonomy with the proper labels.
2.) Attach it to post type "Pages (WP Core)"
3.) You then assign the pages that use this custom page template to that specific taxonomy.
4.) On the Header Element, go to Display rule tab.
5.) On the display rule Location, the label for the custom_page_template should appear.
6.) Select that so you can apply the header element to the pages w/ the certain page template.
Maybe there is an easier way of achieving what I want.
What I am trying to do is I have about 100 pages that I need to have a certain Page hero element on.
I am looking for the best way to do this without having to add each page to the display rules in elements.
Is this possible?
Hi there,
you can try this:
1. Create your Header Element but do not set the Display Location. Whilst you're editing the element, get the ID from the browser URL.
Publish the Element.
2. Then add this PHP Snippet:
add_filter( 'generate_header_element_display', function( $display, $element_id ) {
if ( 10 === $element_id && is_page_template( 'your_template.php' ) ) {
$display = true;
}
return $display;
}, 10, 2 );
You will need to update 10 to the Elements ID and the your_template.php for your template.
Thanks David. The only issue I see is that it will not let me publish an element without a display rule.
Cheers,
Dave
You can save it without a location - it should just provide this waning:
This element needs a location set within the Display Rules tab in order to display.
But it should still publish