WordPress.org

Ready to get started?Download WordPress

Forums

dynamic_sidebar Woes (14 posts)

  1. Joshua Lyman
    Member
    Posted 5 years ago #

    I am about ready to pull some hair out in regards to the dynamic_sidebar function, called to stick in a dynamic sidebar. With one of my sidebars, I only want a layer to appear if the sidebar actually has some widgets defined.

    However, with the typical code that one uses for sticking in the sidebar, it only gives the option for what to output if there are no widgets defined.

    <?php if ( !function_exists('dynamic_sidebar')
            || !dynamic_sidebar('Main Sidebar (Left)') ) : ?>

    I have been trying to rewrite it so that dynamic_sidebar is not called directly, but no matter what I do, whether assign the output to a variable or whatever, it seems to simply throw out its code anyway, regardless of what I want to do with it.

    Can anyone provide any more details on how this function works, and how I can work with it? Thanks!!

  2. RoseCitySister
    Member
    Posted 5 years ago #

    You could do this with text widgets. If you take out ALL html code from sidebar.php, i.e. div id=sidebar, and put it into text widgets at the top and bottom of the sidebar:

    top widget:

    <div id="sidebar">

    bottom widget:

    </div>

    Sidebar would be floated to the left or right in your stylesheet.

    There might be a much better way of doing this, but this is how I would probably do something like this. Either that, or just make a separate template sans sidebar...

  3. Joshua Lyman
    Member
    Posted 5 years ago #

    That's a very good, unique idea! Unfortunately, I'm developing a theme, and so I can't really ask users to create extra text widgets just so that the sidebar might display correctly. But I'm going to keep this idea in the hat for a while, I think it could come in handy.

    Does anybody have any clues as to how dynamic_sidebar() works?

  4. chaoskaizer
    Member
    Posted 5 years ago #

    try the following methods

    add inside functions.php

    function is_sidebar_active( $index = 1){
    	$sidebars	= wp_get_sidebars_widgets();
    	$key		= (string) 'sidebar-'.$index;
    
    	return (isset($sidebars[$key]));
    }

    Place inside your template like so

    <?php
     if ( function_exists('dynamic_sidebar')
     && is_sidebar_active(1) ) {
      // there is active widgets for sidebar-1
      // do something
      echo '<ul id="sidebar-1" class="xoxo">'.PHP_EOL;
      dynamic_sidebar(1);
      echo '</ul>'.PHP_EOL;
     } else {
       // no widget active for sidebar 1
       // do custom sidebar stuff ...
    
     }
    ?>
  5. RoseCitySister
    Member
    Posted 5 years ago #

    Please do post and let us know if that works.

  6. Joshua Lyman
    Member
    Posted 5 years ago #

    I will, I won't have the opportunity till Monday tho, but thanks for the advice! Results will be forthcoming.

  7. Joshua Lyman
    Member
    Posted 5 years ago #

    Okay, great code, that worked exactly as I needed it to! I was going to take a couple minutes to modify the code so that I could also pass it the name of the sidebar, instead of just the id, but then I got busy and couldn't keep that as a priority. Thanks chaoskaizer!

  8. David Yeiser
    Member
    Posted 5 years ago #

    This method of hiding widget sidebars is very cool. However it seems to throw an error in my post navigation. Instead of the post navigation navigating through all of the posts, it is stuck on two specific posts. Even if I'm on one of the two posts that it's stuck on, it still shows that post as the next one to navigate to.

    When I remove:

    function is_sidebar_active( $index = 1){
    	$sidebars	= wp_get_sidebars_widgets();
    	$key		= (string) 'sidebar-'.$index;
    
    	return (isset($sidebars[$key]));
    }

    the post navigation works fine.

    Is there anything in this method that would cause this? Or do you think I've made an error elsewhere.

    Thanks!

  9. David Yeiser
    Member
    Posted 5 years ago #

    Disregard the previous post. It was something else.

  10. viwiv
    Member
    Posted 5 years ago #

    Hi,

    These methods are very interesting, but doesn't work on my side... Due to the 2.7 version of WordPress?

    I use this code in functions.php:

    function is_sidebar_active( $index = 1){
    	$sidebars	= wp_get_sidebars_widgets();
    	$key		= (string) 'sidebar-'.$index;
    
    	return (isset($sidebars[$key]));
    ]
    register_sidebar(array('name'=>'header_right',
            'before_widget' => '<li id="%1$s" class="widget %2$s">',
            'after_widget' => '</li>',
            'before_title' => '<h3 class="widgettitle">',
            'after_title' => '</h3>',
     ));
    register_sidebar(array('name'=>'header_bottom',
            'before_widget' => '<li id="%1$s" class="widget %2$s">',
            'after_widget' => '</li>',
            'before_title' => '<h3 class="widgettitle">',
            'after_title' => '</h3>',
     ));

    I use this code in header.php:

    if ( function_exists('dynamic_sidebar') && is_sidebar_active(1) ) {
    		echo '<ul id="widget_header_right">'. "\n";
    		dynamic_sidebar(1);
    		echo '</ul>'. "\n";
    		}
    		else
    		{
    		}
    ...
    if ( function_exists('dynamic_sidebar') && is_sidebar_active(2) ) {
    		echo '<ul id="widget_header_bottom">'. "\n";
    		dynamic_sidebar(2);
    		echo '</ul>'. "\n";
    		}
    		else
    		{
    		}

    When dynamic-sidebar(2) is empty, ul element displays!

    I check my datatabase in wp-options. Sidebar_widgets displays:

    a:3:{s:9:"sidebar-1";a:1:{i:0;s:6:"images";}s:9:"sidebar-2";a:0:{}s:13:"array_version";i:3;}

    Thanks for your help!!!

  11. viwiv
    Member
    Posted 5 years ago #

    I found the solution in theme "Options" written by Justin Tadlock. To respect copyright, I will not publish the solution here... but it exists!

  12. Batric
    Member
    Posted 5 years ago #

    Hi,

    I am also trying to use the script but it works for me as the regular one.

    Here's my code:
    functions.php

    function is_sidebar_active( $index = 1){
    	$sidebars	= wp_get_sidebars_widgets();
    	$key		= (string) 'sidebar-'.$index;
    
    	return (isset($sidebars[$key]));
    }

    sidebar.php

    <?php if ( function_exists('dynamic_sidebar') && is_sidebar_active() ) { // there are active widgets for this sidebar
                echo '<div class="widgetarea"><ul id="sidebarwidgeted">' . "\n";
                dynamic_sidebar(1);
                echo '</ul></div>';
    } ?>

    It works same as default widget caller - it leaves blank frame if I remove all widgets from sidebar.

    Any suggestions?

    Thanks in any case :)

  13. TechGremlin
    Member
    Posted 4 years ago #

    Don't know if anyone is still having this problem, but I got the above code that chaoskaizer posted working by adding in && dynamic_sidebar(1) to the code, previously the if statement would never return false, causing the sidebar to display nothing, instead of displaying the code inside the else statement.

    Sidebar code:

    <?php
     if ( function_exists('dynamic_sidebar')
     && dynamic_sidebar(1) && is_sidebar_active(1) ) {
      // there is active widgets for sidebar-1
      // do something
      echo '<ul id="sidebar-1" class="xoxo">'.PHP_EOL;
      dynamic_sidebar(1);
      echo '</ul>'.PHP_EOL;
     } else {
       // no widget active for sidebar 1
       // do custom sidebar stuff ...
    
     }
    ?>

    functions.php doesn't need any further editing.

  14. TechGremlin
    Member
    Posted 4 years ago #

    I've just realised this has been replaced by is_active_sidebar() in the new API, please disregard my above post

Topic Closed

This topic has been closed to new replies.

About this Topic