[Resolved] different sidebar layout on different category archives

Home Forums Support [Resolved] different sidebar layout on different category archives

Home Forums Support different sidebar layout on different category archives

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #178621
    Helen

    Hi Tom
    Still loving GP. I had to work with another theme for a while and came back to GP with a sigh of relief!
    In my current project I want to have a default right sidebar but for a couple of categories I want to give more detailed navigation info.
    To make that clear to users I want to switch the sidebar to left. I can do all that easily for individual posts BUT what about the archive pages for the categories whose posts have the left sidebar?
    I would like the archive pages also to have a left sidebar, while also leaving the default right sidebar layout for the other categories.
    Is there a way to set a different sidebar layout for different categories? I have searched and found many plugins that allow customisation of the content of sidebars but so far no luck with this.

    Hope you can help?
    Thanks
    Helen

    #178659
    Tom
    Lead Developer
    Lead Developer

    Hi Helen,

    You can use the built in sidebar filter for this:

    add_filter( 'generate_sidebar_layout','generate_custom_category_sidebar_layout' );
    function generate_custom_category_sidebar_layout( $layout )
    {
     	// If we are on a category, set the sidebar
     	if ( is_category( 'Category Name' ) )
     	 	return 'left-sidebar';
    
     	if ( is_category( 'Another Name' ) )
     	 	return 'right-sidebar';
    
     	if ( is_category( 'One more name' ) )
     	 	return 'no-sidebar';
    
     	// Or else, set the regular layout
     	return $layout;
    
     }

    Adding PHP: https://generatepress.com/knowledgebase/adding-php-functions/

    Let me know if you need more info πŸ™‚

    #178670
    Helen

    Groovy!
    Works well.
    When you say ‘built in’ – was there somewhere I could have known about this? I don’t think I have seen it alluded to. Maybe there are other built in things I should know about as well?
    Thanks.

    #178680
    Tom
    Lead Developer
    Lead Developer

    It’s shown in this knowledgebase article πŸ™‚
    https://generatepress.com/knowledgebase/choosing-sidebar-layouts/

    #179032
    Helen

    Ah yes, I do remember seeing that.
    I’m afraid I am not knowledgable enough to adapt your general examples so I would still have had to ask you about my case.

    Thanks for helping – it is working well now.
    Helen

    #179067
    Tom
    Lead Developer
    Lead Developer

    That’s what I’m here for! Glad I could help πŸ™‚

    #195824
    Helen

    Hi Tom
    A strange thing. I wanted to add some additional categories to use the left sidebar instead of right – but when I click on SIMPLE PHP (which is where I think I pasted your snippet above a few months back?) is says You do not have sufficient permissions to access this page.
    Has something changed, or have I mis-remembered what I did with your snippet above?
    Thanks
    Helen

    #195831
    Helen

    Hello again – I forgot the second part of my question.
    Useful as your snippet was for me, I am really surprised there isn’t a plugin to make it easy to choose which sidebar template gets used for different category archives. There are so many amazing plugins these days that make apparently far more complex tasks easy
    But Ive searched high and low and I can’t find one. Am I missing something?

    #195892
    Tom
    Lead Developer
    Lead Developer

    It would be tough to have an option for every category – what about sites with hundreds of them? That would be hundreds of additional options in the Customizer, which would definitely slow things down considerably.

    Odd about the error message – what about when you go to “Plugins > Editor” and choose “Simple PHP” from the dropdown at the top the top right?

    Then choose custom.php and you should see your old snippets.

    #195944
    Helen

    Ah – got it.
    I didn’t see the plugin editor – started to panic – then suddenly remembered I had a security setting to disable php editing. Unchecked that and now Simple php works fine again. Duh.

    If I want to have several categories using left sidebar, is there a way to combine them all into one rule, or should I write a separate rule for each one?

    Is there a better way to use different sidebar layouts on category archive pages? It is hard for me to see why this is more complex than changing the sidebar template for each single post, which is just a check box?

    I guess I don’t absolutely need it if it slows down my site. I could use right sidebar for all archive pages. I just feel it is helpful for users if the archive pages have the same sidebar layout as the single posts.

    Any thoughts you have much appreciated!

    #195980
    Tom
    Lead Developer
    Lead Developer

    You can use the is_archive() conditional to target all archives.

    You can combine all of your conditionals into the one function to set different sidebar layouts for different categories etc.. It won’t slow down your website at all.

    #195995
    Helen

    Thanks Tom
    So would I separate the categories with a comma? Like below? Or just leave a space?

    if ( is_category( ‘Category 1’, ‘Category 2’ )
    return ‘left-sidebar’;

    #196161
    Tom
    Lead Developer
    Lead Developer
    #196213
    Helen

    Does indeed – and I was right!
    I love the way you always teach us something instead of just telling us the answer.
    Thanks Tom

    #196219
    Tom
    Lead Developer
    Lead Developer

    Glad I could help πŸ™‚

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