🎉 Happy 4th of July! Get 15% off GP Premium with the discount code: 4THOFJULY2020

[Resolved] Display specific widget for custom post type

Home Forums Support Display specific widget for custom post type

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1156843
    Elizabeth

    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.

    #1156854
    Leo
    Staff
    Customer Support

    Hi 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 🙂

    #1156868
    Elizabeth

    Thanks 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?

    #1157204
    Tom
    Lead Developer
    Lead Developer

    Hi 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 🙂

    #1158074
    Elizabeth

    Thanks 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?

    #1158083
    Elizabeth
    #1158420
    Tom
    Lead Developer
    Lead Developer

    Hi 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 🙂

    #1159118
    Elizabeth

    That 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.

    #1159298
    Tom
    Lead Developer
    Lead Developer

    You’re welcome! Glad you’re enjoying GP 🙂

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.