Archived topic
different sidebar layout on different category archives
14 replies · Started by Helen on March 11, 2016
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
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 :)
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.
It's shown in this knowledgebase article :)
https://generatepress.com/knowledgebase/choosing-sidebar-layouts/
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
That's what I'm here for! Glad I could help :)
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
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?
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.
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!
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.
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';
This might help: https://codex.wordpress.org/Conditional_Tags#A_Category_Page
Does indeed - and I was right!
I love the way you always teach us something instead of just telling us the answer.
Thanks Tom
Glad I could help :)
