How to automatically add a class to body_class if there’s a sidebar

To apply the hack, just paste the code below into your functions.php file.

function wpfme_has_sidebar($classes) {
    if (is_active_sidebar('sidebar')) {
        // add 'class-name' to the $classes array
        $classes[] = 'has_sidebar';
    }
    // return the $classes array
    return $classes;
}
add_filter('body_class','wpfme_has_sidebar');

Thanks to WP Function for the tip!

Leave a Reply

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