- This topic has 11 replies, 2 voices, and was last updated 3 years, 3 months ago by Leo.
-
AuthorPosts
-
December 18, 2020 at 7:13 pm #1586613Joshua
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!
December 18, 2020 at 9:11 pm #1586667LeoStaffCustomer SupportHi there,
Would you be able to use a layout element for that?
https://docs.generatepress.com/article/layout-element-overview/#sidebarLet me know π
December 18, 2020 at 10:07 pm #1586697JoshuaHi 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.
December 19, 2020 at 1:20 pm #1587476LeoStaffCustomer SupportYou can use this filter here:
https://docs.generatepress.com/article/generate_layout_element_display/December 19, 2020 at 4:41 pm #1587598JoshuaHey 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?December 19, 2020 at 5:06 pm #1587608LeoStaffCustomer SupportYup that should work:
https://codex.wordpress.org/Conditional_Tags#Is_a_Page_TemplateDecember 19, 2020 at 5:13 pm #1587611JoshuaOkay, 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?
December 19, 2020 at 7:49 pm #1587659LeoStaffCustomer SupportTry choosing content/sidebar in the layout element:
https://docs.generatepress.com/article/layout-element-overview/#sidebarYou can ignore the display rules.
Then add the PHP snippet and make sure to update the
10
in10 === $element_id
December 23, 2020 at 1:00 pm #1592503JoshuaThanks Leo!
I’m going to give this a shot this evening and let you know if it worked. Appreciate your help as always!
December 23, 2020 at 4:06 pm #1592630LeoStaffCustomer SupportSounds good.
December 26, 2020 at 8:09 pm #1595280JoshuaHey 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!
December 26, 2020 at 8:15 pm #1595288LeoStaffCustomer SupportAwesome note on the single part.
Thanks for sharing π
-
AuthorPosts
- You must be logged in to reply to this topic.