- This topic has 9 replies, 2 voices, and was last updated 4 years, 1 month ago by
David.
-
AuthorPosts
-
July 27, 2021 at 1:47 pm #1873475
Sean
Hi! I have a custom template from our previous theme that should be loading the sidebar, but it isn’t (see code below). I believe the problem is that the name of the sidebar may be different in GeneratePress. However, since GeneratePress doesn’t have “sticky” sidebar options, I’m wondering if there’s an even simpler snippet available that we could add to our template that will load the GeneratePress sidebar. In our case, we have Sidebar Layout in the GP options set to “Content / Sidebar”.
Is there a snippet of code we can paste into our template that’ll load the Sidebar Layout?
Thank you,
Seanpublic function runners_result_single_template( $single_template ) { global $post; $rr_class_name = $this->rr_class_name; if ( $post->post_type == $rr_class_name::EVENT_POST_TYPE ) { add_filter('body_class', function ( $classes ) { $classes[] = 'has-sidebar'; return $classes; }); add_action( 'wp_head', array( $this, 'add_share_meta_tags' ), 0 ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts_single_result_page' ) ); $single_template = plugin_dir_path( __FILE__ ) . 'templates/single-runners-result.php'; } return $single_template; }
Code from templates/single-runners-result.php:
<?php if( function_exists( 'generated_dynamic_sidebar' ) ) : ?> <?php $css_classes = 'sidebar'; $option = get_option('fusion_options'); if($option) { // $fusion_options = unserialize($option); // var_dump($option); if($option['sidebar_sticky'] == 'sidebar_one' || $option['sidebar_sticky'] == 'both') { $css_classes .= ' fusion-widget-area fusion-content-widget-area fusion-sidebar-right fusion-blogsidebar fusion-sticky-sidebar'; } } ?> <div id="sidebar" role="complementary" class="<?php echo $css_classes; ?>"> <div class="fusion-sidebar-inner-content"> <?php generated_dynamic_sidebar( 'Blog Sidebar' ); ?> </div> </div> <?php endif; ?>
July 28, 2021 at 7:57 am #1874707David
StaffCustomer SupportHi there,
is it possible to see a link to the post so i can see the template structure?
July 28, 2021 at 8:49 am #1874790Sean
Good morning David,
Thanks for getting back to me. Sure, I am including a link in the Private Info section (because this work is currently being done on my staging site). Thanks!
July 29, 2021 at 3:52 am #1875534David
StaffCustomer SupportQuick question – what are the Sidebar Settings in Customizer > Layout > Sidebars ?
What happens if they are enabled on ALL options.July 29, 2021 at 6:06 am #1875664Sean
Hi David. I already have the Sidebar Layout, Blog Sidebar Layout, and Single Post Sidebar Layout set to “Content / Sidebar” so I think the problem is that I need the appropriate code in my template in order for those pages to display the sidebar.
The above code was taken from the respective template files, but if it’d be helpful to see the complete files, I can share those. Just let me know.
July 30, 2021 at 5:40 am #1876998David
StaffCustomer SupportWhat happens if we try this:
add_filter( 'generate_sidebar_layout', function( $layout ) { // If we are viewing the custom post type, set the sidebar if ( get_post_type() ==='your_single_cpt_slug' ) { return 'right-sidebar'; } // Or else, set the regular layout return $layout; } );
Just need to add the correct
your_single_cpt_slug
July 30, 2021 at 8:05 am #1877373Sean
Thanks for that filter David. I gave it a try in both the template file as well as in functions.php, but unfortunately, I’m still not seeing the sidebar on the page. I’m attaching a link to the complete template file in the Private Information in case that helps in troubleshooting this problem. I made comments in the file where I added your code as well as where the “old” sidebar code is, immediately below yours. If you need any more info, please let me know!
July 30, 2021 at 10:10 am #1877523David
StaffCustomer SupportTo keep things playing nice with GP it would be better to make a copy of the GP single.php
https://github.com/tomusborne/generatepress/blob/master/single.php
And repurpose it for your needs.
Ideally you would want to keep as much of the GP Template structure, and simply replace the Loop with your post templates functions – ie. these lines here:
You won’t need any of the Sidebar code in there as the GP provides the hooks for adding in the GP Template.
August 2, 2021 at 8:16 am #1880832Sean
Thanks David! Replacing the code before/after the content portion of my template with the code from the single.php template got the sidebar to show up on my template. Looking good now. Really appreciate you pointing me in the right direction!
Best regards,
SeanAugust 2, 2021 at 6:14 pm #1881320David
StaffCustomer SupportGlad to be of help!
-
AuthorPosts
- You must be logged in to reply to this topic.