Ready to get started?Download WordPress


[resolved] is_dynamic_sidebar to return FALSE with multiple sidebars (7 posts)

  1. Minkowski
    Posted 5 years ago #

    I'm building a site using wordpress that uses multiple sidebars - Header, Left, Footer, etc.

    What I want to do is to test whether there are any active widgets within any particular sidebar so that I don't load the enclosing < ul > tags if there are none, and more besides, I also want a dynamic layout based on these tests.

    The problem is that I would have thought that is_dynamic_sidebar('header') would return TRUE if there was only an active widget within the dynamic_sidebar('header'), but it seems as though it doesn't accept a value and returns TRUE wherever a widget is placed within my sidebars.

    Any help would be appreciated.

  2. Minkowski
    Posted 5 years ago #

    OK. There is probably a built in way but this is suiting me until I find it.

    I've built the function -

    function widgets_exist($sidebar){
    	$sidebars_widgets = get_option('sidebars_widgets');
    	if (count($sidebars_widgets[$sidebar])){
    		return true;
    		return false;

    and placed that into the functions.php and calling it in the templates with -

    if (widgets_exist('header')) echo "TRUE";

    As I said, it will do for now but I would have thought that there would be a built in way or a more elegant solution. But in the meantime if this helps anyone else that's great.

  3. Minkowski
    Posted 5 years ago #

    Wow, how stupid do I feel?

    if(dynamic_sidebar('header')) echo "you're quite stupid";

    Pretty stupid.

  4. monkeyangst
    Posted 5 years ago #

    Not stupid at all. The thing is, the dynamic_sidebar function does return FALSE if the sidebar isn't dynamic, but if it IS, it seems to echo the whole dynamic sidebar. I can't seem to make that work in an either/or scenario, so I'm going with your solution.

  5. Justin Tadlock

    Posted 5 years ago #

    Yeah, dynamic_sidebar() does will not only return TRUE, but it will echo the entire widget area.

    I've been using this for some time now:

    if ( is_sidebar_active( 'header' ) ) // Do something
    function is_sidebar_active( $index = 1 ) {
    	global $wp_registered_sidebars;
    	if ( is_int( $index ) ) :
    		$index = "sidebar-$index";
    	else :
    		$index = sanitize_title( $index );
    		foreach ( (array) $wp_registered_sidebars as $key => $value ) :
    			if ( sanitize_title( $value['name'] ) == $index ) :
    				$index = $key;
    	$sidebars_widgets = wp_get_sidebars_widgets();
    	if ( empty( $wp_registered_sidebars[$index] ) || !array_key_exists( $index, $sidebars_widgets ) || !is_array( $sidebars_widgets[$index] ) || empty( $sidebars_widgets[$index] ) )
    		return false;
    		return true;

    I'll have to try the solution above as well.

  6. ravisarma
    Posted 5 years ago #

    I think is_active_sidebar() will tell you if the sidebar has any widgets ("is active"). Caution: though the codex suggests that it takes as arg a sidebar ID or name, using name does not seem to work. This seems to have been reported in Trac and a patch provided a while ago, but I am not sure where that stands.

  7. amoswright
    Posted 4 years ago #

    had same problem: is_active_sidebar() wasn't working with sidebar name... but ravisarma is right, it works with ID. there was just one gotcha for me before getting it to work, pass the id without quotes, for example is_active_sidebar(3) works but is_active_sidebar('3') does not. thanks for the help all!

Topic Closed

This topic has been closed to new replies.

About this Topic