Check if Sidebar has Widgets

Check if Sidebar has Widgets

? The Snippet:

if ( is_active_sidebar( 'name-of-sidebar' ) ) {

    // Show Content
    
}

ℹ️ About the Snippet

Using this code snippet, you can check if a sidebar is active or not. In other words, if sidebar has any widgets in it or not. So, if there are any widgets in it. Then, it’ll return as TRUE. However, if there are no widgets in the given sidebar, it’ll return as FALSE. This snippet is useful if you want to hide that sidebar if there’s no widget in it on your WordPress website.

✅ How to use the Snippet

For example the id of sidebar is “right-sidebar” and you want to call a sidebar after checking, below will be an example of that snippet:

To clarify, as this is a boolean function, that means it will return TRUE & FALSE about the given sidebar. To use this code snippet, you’ll first have to find the id of the sidebar you want to check.

<?php if ( is_active_sidebar( 'right-sidebar' ) ) { ?>
    <ul id="sidebar">
        <?php dynamic_sidebar( 'right-sidebar' ); ?>
    </ul>
<?php } ?>

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get regular WordPress updates directly in your inbox.

shares