X

How To Only Show a Widget Area If It Contains Widgets

Snippets by IsItWP

Are you looking for a way to only show a widget area if it contains widgets? This snippet will check whether a widget area contains any widgets or not, and only displays the widget area if it contains widgets.

Instructions:

  1. Replace a widget area, such as the sidebar, with the following snippet.
  2. Change the name sidebar-name in lines 1 and 3 to the name of the widget you wish to hide if it has no widgets.
<?php if( is_active_sidebar('sidebar-name') ) { ?>
  <ul class="widgets-container">
    <?php dynamic_sidebar('sidebar-name'); ?>
  </ul><!-- /widgets-container -->
<?php } ?>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: CSS Hero Review: #1 WordPress Theme Customization Plugin.

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!