Site logo

Archived topic

Sidebar in Custom Template

9 replies · Started by Sean on July 27, 2021

Viewing posts 1–10 of 10

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,
Sean

	public 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; ?>

Hi there,

is it possible to see a link to the post so i can see the template structure?

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!

Quick question - what are the Sidebar Settings in Customizer > Layout > Sidebars ?
What happens if they are enabled on ALL options.

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.

What 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

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!

To 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:

https://github.com/tomusborne/generatepress/blob/2c7c8a76d3b58b0b97e82b3a7a5ebf022a1ddfbe/single.php#L24-L32

You won't need any of the Sidebar code in there as the GP provides the hooks for adding in the GP Template.

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,
Sean

Glad to be of help!

This archived topic is closed to new replies.