Site logo

Archived topic

Not show the sidebar when there's no widgets

11 replies · Started by Ivan Cazorla on May 9, 2019

Viewing posts 1–12 of 12

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?

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'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!

This archived topic is closed to new replies.