- This topic has 14 replies, 2 voices, and was last updated 7 years, 10 months ago by Tom.
-
AuthorPosts
-
March 11, 2016 at 2:23 pm #178621Helen
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
HelenMarch 11, 2016 at 11:11 pm #178659TomLead DeveloperLead DeveloperHi 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 π
March 11, 2016 at 11:50 pm #178670HelenGroovy!
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.March 12, 2016 at 12:12 am #178680TomLead DeveloperLead DeveloperIt’s shown in this knowledgebase article π
https://generatepress.com/knowledgebase/choosing-sidebar-layouts/March 13, 2016 at 2:01 pm #179032HelenAh 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.
HelenMarch 13, 2016 at 7:29 pm #179067TomLead DeveloperLead DeveloperThat’s what I’m here for! Glad I could help π
May 22, 2016 at 12:16 am #195824HelenHi 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
HelenMay 22, 2016 at 1:23 am #195831HelenHello 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?May 22, 2016 at 10:07 am #195892TomLead DeveloperLead DeveloperIt 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.
May 22, 2016 at 2:09 pm #195944HelenAh – 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!
May 22, 2016 at 11:51 pm #195980TomLead DeveloperLead DeveloperYou 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.
May 23, 2016 at 12:42 am #195995HelenThanks 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’;May 23, 2016 at 10:24 am #196161TomLead DeveloperLead DeveloperThis might help: https://codex.wordpress.org/Conditional_Tags#A_Category_Page
May 23, 2016 at 2:42 pm #196213HelenDoes indeed – and I was right!
I love the way you always teach us something instead of just telling us the answer.
Thanks TomMay 23, 2016 at 3:46 pm #196219TomLead DeveloperLead DeveloperGlad I could help π
-
AuthorPosts
- You must be logged in to reply to this topic.