2

Is possible to check if has any sidebar active on current page?

Like this:

@edit

This function also return true even if the current page does not have this sidebar active...

<?php if(is_active_sidebar('sidebar-1')) {
// sidebar-1 is active...
} ?>

But I need to check if has any sidebar active (without specify sidebar name ), and I need to check only to current page, not for all pages, like:

<?php if(any_sidebar_active_on_this_page()) {
// has sidebar active on this specific page, do something...
} ?>
2

I believe the issue here is a problem with vernacular.

is_active_sidebar isn't page specific -- just checks to see if it has data within it throughout the entire site.

You are asking a different question. Sounds like you are asking "does this template reference and sidebars AND do any of those sidebars have data in them,?" it also sounds like you don't want to reference the sidebar names at all and you'd like this to be a generic question across the board.

That last bit makes this difficult and can potentially turn this into an architectural problem -- not a WordPress problem. Think of WordPress as a waiter -- it just takes your orders and gets you results. It wasn't built for you to ask questions like "is anyone in the restaurant eating steak?" -- so it doesn't have anything for you there.

You COULD tie into https://codex.wordpress.org/Plugin_API/Action_Reference/dynamic_sidebar

Then you could set a variable or call a function anytime someone calls a sidebar that has data in it -- this would allow you to react in those moments.

  • I'd like to write a function to add .has-sidebar to the body_class() only if the current template references a specific sidebar. Is this possible? I have the function mostly written; I just need a conditional statement to check if the current template references a specific sidebar--regardless of whether there are any active widgets. – Paul Shryock Dec 26 '17 at 17:30
0
foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) {    
    if(is_active_sidebar($sidebar['name'])) {
        // $sidebar['name'] is active...
    }
}

This will loop through the list of registered sidebars and then check on every page load if any of them exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.