views:

31

answers:

2

I'm trying to wrap the sidebar by a DIV, and if the sidebar is empty the DIV should not be displayed

But i cannot use codes like

if(dynamic_sidebar(1))
{
  echo '<div>';
  dynamic_sidebar(1);
  echo '</div>';
}

as it will load the sidebar before the DIV if it is not empty, any ideas?

A: 

Try:

if ( is_active_sidebar(1) )
{
  echo '<div>';
  dynamic_sidebar(1);
  echo '</div>';
}
Greenie
it doesn't work
Edward
+1  A: 

You can always use output buffering. When output buffering is on, anything which would normally be echoed to the screen is instead stored in a buffer. You can then test to see if there is anything in the buffer before outputting your div tags.

ob_start();
dynamic_sidebar(1);
$sidebar = ob_get_clean();  // get the contents of the buffer and turn it off.
if ($sidebar) {
    echo "<div>" . $sidebar . "</div>";
}
nickf