[Support request] Specifc sidebars for custom post types

Home Forums Support [Support request] Specifc sidebars for custom post types

Home Forums Support Specifc sidebars for custom post types

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1947423
    Josh

    I’m attempting to create separate (unique) sidebar content for my custom post type single pages. All sidebars are currently off on the site (content-only).

    I created a new side bar using this code:

    //add new sidebar
    function rt_custom_sidebars() {
        register_sidebar(array(
            'name' => 'Team Sidebar',
            'id' => 'team-sidebar',
            'description' => 'sidebar for team single pages',
            'before_widget' => '<div class="teamwidget">',
            'after_widget' => '</div>',
            'before_title' => '<h3>',
            'after_title' => '</h3>',
            )
        );
    }
    add_action( 'widgets_init', 'rt_custom_sidebars' );

    This creates the widget fine for me.

    I was hoping to assign it to single custom post type (‘pacific_inns_member”).

    I tried with this code:

    //choose sidebars for custom posts
    add_filter( 'generate_sidebar_layout', function( $layout ) {
     	// If we are on a an event page
        if ( is_singular('pacific_inns_member') ) {
            return 'left-sidebar';
        }
        // Or else, set the regular layout
        return $layout;
     } );

    This successfully adds the left-sidebar. However if i change the line return 'left-sidebar'; to return 'team-sidebar'; Nothing is displayed.

    What is the generatepress method for adding my new sidebar to only the single page of my custom post type?
    (I am currently using a template “single-pacific_inns_member.php”. and tried replacing generate_construct_sidebars() but that eliminated formatting.)
    Is there a filter to add an if statement to this function?

    #1947636
    Elvin
    Staff
    Customer Support

    hi Josh,

    I believe what you need to do is to modify what the add_action( 'generate_sidebars', 'generate_construct_sidebars' );
    https://github.com/tomusborne/generatepress/blob/b60b853630da6d9015722da903e53c8064148b0a/inc/structure/sidebars.php#L12-L43

    This is because it won’t matter if the filter returns team-sidebar if there’s no actual get_sidebar( 'team-sidebar' ); function to render the registered sidebar for you on the action hook for constructing the actual registered sidebar.

    I’d suggest removing the default add_action( 'generate_sidebars', 'generate_construct_sidebars' ); by doing a
    remove_action( 'generate_sidebars', 'generate_construct_sidebars' ); and then re-add the same function but modify it including your get_sidebar( 'team-sidebar' ); condition. 😀

    #1948467
    Josh

    Thanks for the help with this Elvin. I believe I have it working, so I’m posting what I did in case anyone else needs it and to confirm with the GP team that this is in fact the way it should have been done.

    1. I replaced the above code in my first post to make my sidebar with this code to address formatting issues (changed from DIV to ASIDE and added classes):

    //add new sidebar(s)
    function rt_custom_sidebars() {
        register_sidebar(array(
            'name' => 'Team Sidebar',
            'id' => 'team-sidebar',
            'description' => 'sidebar for team single pages',
            'before_widget' => '<aside id="%1$s" class="widget inner-padding %2$s teamwidget ">',
            'after_widget' => '</aside>',
            'before_title' => '<h3>',
            'after_title' => '</h3>',
            )
        );
    }
    add_action( 'widgets_init', 'rt_custom_sidebars' );

    2. Used the code generate_sidebar code to assign the left-sidebar to my single post types as I don’t have a sidebar for the rest of the site.

    3. I added this code to my functions.php

    add_action( 'wp','rt_team_sidebar' );
    function rt_team_sidebar()
    {
        if ( is_singular('pacific_inns_member') ) {
            remove_action( 'generate_sidebars','generate_construct_sidebars' );
            add_action( 'generate_sidebars','rt_construct_sidebar' );
        }
    }
    
    function rt_construct_sidebar() {
        get_sidebar( 'team' );
    }

    (note Elvin suggested using get_sidebar( 'sidebar-team' ) but it should be get_sidebar( 'team' ).)

    4. Next I copied the sidebar-left.php from my parent theme and put into my child theme. Renamed it to sidebar-team.php and changed this line:

    if ( ! dynamic_sidebar( 'sidebar-2' ) ) {
        generate_do_default_sidebar_widgets( 'left-sidebar' );
    }

    to

    if ( ! dynamic_sidebar( 'team-sidebar' ) ) {
        generate_do_default_sidebar_widgets( 'team-sidebar' );
    }

    5. Last, in my single-pacific_inns_member.php I changed this line:

    generate_construct_sidebars();

    to

    rt_construct_sidebar();

    This seems to be working. I’d really like a confirmation that it looks correct though. Specifically on the if statement in rt_team_sidebar().

    #1948816
    Elvin
    Staff
    Customer Support

    Nice one! That’s good use of the hook.

    Glad you got it sorted. Thanks for sharing it with us. 😀

    #1949935
    Josh

    So. Big question here, but I think this why I couldn’t wrap my head around this.

    It says that the hook for add_action( 'generate_sidebars','generate_construct_sidebars' ); was removed in 2.0. That’s why i had to edit the templates and couldn’t just leave generate_construct_sidebars() there. Based on this knowledge there was a lot that I didn’t need here.

    The simplest thing to do would have been in my template file to just replace generate_construct_sidebars() with get_sidebar('team') after having created the sidebar-team.php file.

    Wouldn’t that make more sense?

    Or if I didn’t want to adjust template files we could maybe just rewrite the pluggable function generate_construct_sidebars() adding our if then statement there?

    It seems though that all this:

    add_action( 'wp','rt_sidebars' );
    function rt_sidebars()
    {
        if ( is_singular('pacific_inns_member') ) {
            remove_action( 'generate_sidebars','generate_construct_sidebars' );
            add_action( 'generate_sidebars','rt_construct_team_sidebar' );
        }
        if ( is_singular('pacific_inns_service') || is_archive ('pacific_inns_service') || is_page(8) ) {
            remove_action( 'generate_sidebars','generate_construct_sidebars' );
            add_action( 'generate_sidebars','rt_construct_service_sidebar' );
        }
    }

    is actually not doing anything at all?

    #1950026
    Josh

    I tried using the pluggable function generate_construct_sidebars() as well. I addded this to my functions.php

    function generate_construct_sidebars() {
        if ( is_singular('pacific_inns_member')) {
            get_sidebar( 'team' );
        } elseif ( is_singular('pacific_inns_service') || is_archive ('pacific_inns_service') || is_page(8) ) {
            get_sidebar( 'service' );
        } else {
            $layout = generate_get_layout();
    
            // When to show the right sidebar.
            $rs = array( 'right-sidebar', 'both-sidebars', 'both-right', 'both-left' );
    
            // When to show the left sidebar.
            $ls = array( 'left-sidebar', 'both-sidebars', 'both-right', 'both-left' );
    
            // If left sidebar, show it.
            if ( in_array( $layout, $ls ) ) {
                get_sidebar( 'left' );
            }
    
            // If right sidebar, show it.
            if ( in_array( $layout, $rs ) ) {
                get_sidebar();
            }
        }
    }
    //choose sidebars for custom posts
    add_filter( 'generate_sidebar_layout', function( $layout ) {
     	// If we are on a an member page
        if ( is_singular('pacific_inns_member') ) {
            return 'team';
        }
    	// If we are on service pages (id 8 is the main what we do page)
        if ( is_archive('pacific_inns_service') || is_singular('pacific_inns_service') || is_page(8) ) {
            return 'service';
        }
    
        // Or else, set the regular layout
        return $layout;
     } );

    That made all sorts of strange things mostly. only the service sidebar was showing up.

    So I tried adding this:

    function generate_construct_sidebars() {
            $layout = generate_get_layout();
    
            // When to show the right sidebar.
            $rs = array( 'right-sidebar', 'both-sidebars', 'both-right', 'both-left' );
    
            // When to show the left sidebar.
            $ls = array( 'left-sidebar', 'both-sidebars', 'both-right', 'both-left' );
    
            // If left sidebar, show it.
            if ( in_array( $layout, $ls ) ) {
                get_sidebar( 'left' );
            }
    
            // If right sidebar, show it.
            if ( in_array( $layout, $rs ) ) {
                get_sidebar();
            }
    	 if ($layout=="team"){
                get_sidebar('team');
            }
    	 if ($layout=="service"){
                get_sidebar('service');
            }
    }
    
    //choose sidebars for custom posts
    add_filter( 'generate_sidebar_layout', function( $layout ) {
     	// If we are on a an member page
        if ( is_singular('pacific_inns_member') ) {
            return 'team';
        }
    	// If we are on service pages (id 8 is the main what we do page)
        if ( is_archive('pacific_inns_service') || is_singular('pacific_inns_service') || is_page(8) ) {
            return 'service';
        }
    
        // Or else, set the regular layout
        return $layout;
     } );

    This works, but all sidebars (which were copied off sidebar-left) show up on the right side of the page.

    I think i’m just confusting myself at this time…what am I doing wrong?!!?

    #1950881
    Tom
    Lead Developer
    Lead Developer

    Hi Josh,

    What you said here is correct: https://generatepress.com/forums/topic/specifc-sidebars-for-custom-post-types/#post-1949935

    Since you’re already using a child theme, you simply have to replace the generate_construct_sidebars() function call with your get_sidebar( 'team' ) function. That’s it – nothing else should be required.

    Let me know 🙂

    #1950915
    Josh

    Thanks Tom. I tried a LOT of ways to make this work, but what you suggested is the only way that reliably worked. The theme is amazing, but it’s too bad this is the only way. Not faulting the theme though, I tried to figure out a method to make this work and couldn’t. It is too bad though that I have to import a template for each post type and archive to make this work. With 3 CPTs, this was 6 duplication of theme files to make it work.

    #1951638
    Tom
    Lead Developer
    Lead Developer

    Have you considered a plugin like this?: https://wordpress.org/plugins/content-aware-sidebars/

    Alternatively, you could use a Sidebar Block Element: https://docs.generatepress.com/article/block-element-overview/

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