Archived topic
Not show the sidebar when there's no widgets
11 replies · Started by Ivan Cazorla on May 9, 2019
Hi there,
Right now I've set a widget on my sidebar to show the sub categories, as you can see here (https://www.deacampada.com/categ/descanso/)
But when there's no more subcategories to show I would like to hide the entire sidebar, because there's a huge useless white column at the left, as you can see here (https://www.deacampada.com/categ/descanso/colchones/colchones-individuales/)
Is there a way to deactivate the sidebar when there's no widgets on it?
Let me know.
Thanks,
Ivan
Hi there,
Try this filter:
add_filter( 'generate_sidebar_layout', function( $layout ) {
if ( 'left-sidebar' === $layout && ! is_active_sidebar( 'sidebar-2' ) ) {
return 'no-sidebar';
}
return $layout;
} );
Adding PHP: https://docs.generatepress.com/article/adding-php/
Let me know :)
Hi Tom,
Thank you but it doesn't work as I wanted, even when there should be a widget it has disabled the entire sidebar.
If I deactivate the php code you gave me, I can see my widget again.
I only need to deactiate the sidebar when the widget I have inside of it doesn't have any information to show (blank).
Let me know,
Ivan
I just adjusted the code above - can you give it another shot?
Now I can see the sidebar but when there's no widget to show there's still the white column on the left as you can see here (https://www.deacampada.com/categ/mobiliario-camping/sillas-camping/sillas-altas/)
Let me know.
Hmm, something must be altering the core output of the widget. What determines whether something appears in that widget or not? Is it a core widget you're using, or a plugin?
Hi, if there are more subcategories to show it will show them, if not the widget won't show. Actually I think is a widget I can choose from the widgets search, not sure now if it appears by a plugin or not.
let me know
So it's just the regular Categories widget?
I think so, yeah.
I'm not sure if we can determine if the categories widget is empty. Maybe you can force the widget to show the categories no matter what?: https://wordpress.stackexchange.com/a/25002/90661
I've tried that but it didn't worked for me. I've added another widget/filter so that when the sidebar would be empty, now it has some other widgets to show.
Sounds like a good workaround. Sorry I wasn't able to help more!