- This topic has 8 replies, 3 voices, and was last updated 4 years, 2 months ago by Tom.
-
AuthorPosts
-
February 6, 2020 at 8:52 am #1156843Elizabeth
I want to start by thanking you for a fantastic theme!
I’m working on a new website that has a custom post type registered as ‘tdiwebinars’. What I am trying to do is set up the corresponding sidebar-tdiwebinars.php file so that a specific widget loads for single posts for the ‘tdiwebinars’.
The CPT and widget area are already set up in the functions.php file. I have other custom widget areas that are displaying correctly based on page ID or tree by using a custom sidebar.php file (found here: sidebar.php on GitHub.
Here is part of the code that I currently have in the sidebar-tdiwebinars.php file:
if ( is_singular( 'tdiwebinars' ) ) { dynamic_sidebar( 'Webinars Sidebar' ); } else { if ( ! dynamic_sidebar( 'sidebar-1' ) ) : if ( false == $navigation_active ) :
Any help with this is appreciated.
February 6, 2020 at 9:12 am #1156854LeoStaffCustomer SupportHi there,
Would a plugin like this work better?
https://en-ca.wordpress.org/plugins/content-aware-sidebars/This is our usual recommendation.
Let me know π
February 6, 2020 at 9:28 am #1156868ElizabethThanks Leo. That would work but I would rather not use a plugin. I’m comfortable working in the child theme template files and have done this with fully custom themes. Is there something standing out to you in the code above that would keep this from working in GeneratePress?
February 6, 2020 at 4:54 pm #1157204TomLead DeveloperLead DeveloperHi there,
To do this, you would likely need to copy our
sidebar.php
file from the parent theme, and add it to your child theme.Then you would add your conditional check around this part of the code: https://github.com/tomusborne/generatepress/blob/master/sidebar.php#L22-L24
That way you can load your own
dynamic_sidebar( 'abc' )
when your condition is true, and fall back to ours when it isn’t.Let me know if you need more info π
February 7, 2020 at 7:44 am #1158074ElizabethThanks Tom. I apologize because I may not be explaining this well. I’ve set up conditional statements in the sidebar.php page for each major section of the website and these are all working. If none of the conditions are met then the default sidebar shows for pages. It’s the sidebar for the custom post type tdiwebinars that isn’t working.
The custom post type is tdiwebinars with a corresponding single-tdiwebinars.php template. The sidebar that I’m trying to display is webinars_sidebar.
I found this post a few minutes ago and tried it out but must still doing something wrong.
This is what I’ve added to my functions.php file:
add_filter( 'generate_sidebar_layout','cpt_tdiwebinars_sidebar_layout' ); function cpt_tdiwebinars_sidebar_layout( $layout ) { // If we are on a webinars single page, set the sidebar if ( is_singular( 'tdiwebinars' ) ) return 'webinars_sidebar'; // Or else, set the regular layout return $layout; }
This last suggestion seems to be doing something because no sidebar shows at all, just a full-width page. Here’s an example.
Do you see what I’m missing or doing wrong?
February 7, 2020 at 8:02 am #1158083ElizabethTom- here are my files on Gist: https://gist.github.com/candyappledesign/ebfb944e284a7a0b39babbb6cf35a81c
February 7, 2020 at 5:11 pm #1158420TomLead DeveloperLead DeveloperHi there,
That filter (
generate_sidebar_layout
) won’t work in this case.Instead, you need to make a copy of this file: https://github.com/tomusborne/generatepress/blob/2.4.1/sidebar.php
Then add it to your child theme root directory.
Then directly inside the file, you need to replace this:
if ( ! dynamic_sidebar( 'sidebar-1' ) ) { generate_do_default_sidebar_widgets( 'right-sidebar' ); }
With this:
if ( is_singular( 'tdiwebinars' ) ) { dynamic_sidebar( 'your_sidebar_id' ); } else { if ( ! dynamic_sidebar( 'sidebar-1' ) ) { generate_do_default_sidebar_widgets( 'right-sidebar' ); } }
Hope this helps π
February 8, 2020 at 1:29 pm #1159118ElizabethThat worked, thank you so much! I really appreciate your help with this.
And thank you again for a great theme that’s flexible to work with. I like the option of not having to use plugins for things like this.
February 8, 2020 at 8:53 pm #1159298TomLead DeveloperLead DeveloperYou’re welcome! Glad you’re enjoying GP π
-
AuthorPosts
- You must be logged in to reply to this topic.