The Problem ?
If you use widgets in your theme and this also accommodates a variety of hierarchies, you will quickly realize that it really only makes sense to output HTML for the widgets when the widget area is also used by the user.
When you do not have any text to output in widget area it shows blank space. How to solve it ?
Solution
You just need one function, which is similar to the Conditional Tags of WordPress. Put this function in your functions.php of your theme.
1 2 3 4 5 6 7 8 9 10 11 12 13 | function is_sidebar_active($index) { global $wp_registered_sidebars; $widgetcolums = wp_get_sidebars_widgets(); if ($widgetcolums[$index]) return true; return false; } |
It uses the output of your widget area, only if there is some content to show.
A query could look like this in your template:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php if ( function_exists('is_sidebar_active') && is_sidebar_active('sidebar') ) { ?> <div id="sidebar"> <ul> <?php dynamic_sidebar('sidebar'); ?> </ul> </div> <?php } ?> |
The above example loads the content only, if the widget area with the ID sidebar has content, that means if widgets are used in this area.
No More Blank Space now !!



Comments