Archived topic
Show sidebar for specific Post templates
11 replies · Started by Joshua on December 18, 2020
Hi,
Is it possible to show the sidebar for only specific Post templates? For example, I've got three Post templates: Default, Best of, Reviews. All three Post templates are in the default Post post type. If I turn the sidebar on, by default it will turn on for all three Post templates. However, I'd like it to only show on the Reviews Post template. Is this possible?
Thanks guys!
Hi there,
Would you be able to use a layout element for that?
https://docs.generatepress.com/article/layout-element-overview/#sidebar
Let me know :)
Hi Leo!
That would be perfect — is there a way to add new location rules to the Display Rules on the layout element? It looks like there are only rules for taxonomies, but since the post templates aren't taxonomies, they don't appear in the location rules as an option.
Alternatively, if there isn't a way to add custom location rules, I could just use a custom taxonomy. When that taxonomy is added to a post, it can turn the sidebar on or off.
You can use this filter here:
https://docs.generatepress.com/article/generate_layout_element_display/
Hey Leo,
I'm not 100% sure I understand how to use that filter. Would I replace the is_author with
is_page_template and then where it says Tom replace that with the name of my post template?
Yup that should work:
https://codex.wordpress.org/Conditional_Tags#Is_a_Page_Template
Okay, so my code looks like this so far:
add_filter( 'generate_layout_element_display', function( $display, $element_id ) {
if ( 10 === $element_id && is_page_template( 'single-bank-reviews.php' ) ) {
$display = true;
}
return $display;
}, 10, 2 );
I want to show a right sidebar on the single Bank Reviews page template. I've got this code in my functions.php, but how would I go about using the layout element to actually show the sidebar? Or do I not need to use the layout element anymore because this code bypasses it?
Try choosing content/sidebar in the layout element:
https://docs.generatepress.com/article/layout-element-overview/#sidebar
You can ignore the display rules.
Then add the PHP snippet and make sure to update the 10 in 10 === $element_id
Thanks Leo!
I'm going to give this a shot this evening and let you know if it worked. Appreciate your help as always!
Sounds good.
Hey Leo,
It worked, thanks so much!
For anyone else reading this for a solution, keep this in mind:
I was struggling for a few minutes because it wasn't working. But I realized that I had to include the directory that the single template was in. So instead of this code:
add_filter( 'generate_layout_element_display', function( $display, $element_id ) {
if ( 10 === $element_id && is_page_template( 'single-bank-reviews.php' ) ) {
$display = true;
}
return $display;
}, 10, 2 );
I used this code:
add_filter( 'generate_layout_element_display', function( $display, $element_id ) {
if ( 10 === $element_id && is_page_template( 'single/single-bank-reviews.php' ) ) {
$display = true;
}
return $display;
}, 10, 2 );
Notice the single/ added? That fixed it! You would of course have to change single/ to be whatever your directory is called.
Also, the 10 in 10 === $element_id has to be replaced with the ID of the Element you wish to remove. The ID can be found by editing the Element and checking the URL for the "post=".
Thanks so much again, Leo!
Awesome note on the single part.
Thanks for sharing :)