Reputation: 4918
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?
Upvotes: 1
Views: 7498
Reputation: 4000
You can use the WordPress is_dynamic_sidebar(); function.
It returns true
if any registered sidebar contains a widget; otherwise false.
Example:
<?php if(is_active_sidebar('my-sidebar') ) { ?>
<div class="about-us">
<?php dynamic_sidebar('my-sidebar'); ?>
</div>
<?php } ?>
This is the widget-area/sidebar register script I run from the functions.php
:
function foxinni_widgets_init() {
register_sidebar( array(
'name' => 'My Sidebar',
'id' => 'my-sidebar',
'description' => '','before_widget' => '','after_widget' => '','before_title' => '','after_title' => '',
) );
}
add_action( 'widgets_init', 'foxinni_widgets_init' );
Notice that I use the string, my-sidebar
, to identify the sidebar by id.
Upvotes: 2
Reputation: 546095
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>";
}
Upvotes: 4
Reputation: 670
Try:
if ( is_active_sidebar(1) )
{
echo '<div>';
dynamic_sidebar(1);
echo '</div>';
}
Upvotes: 3