Post Pic

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

By default, the body_class() function adds some class to the <body> tag to allow you to style your site more easily. But unfortunely, no extra class is added when your template has a sidebar. Here is a imple function to solve this problem.

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!

Related Posts

Related Posts

No related posts.

2 Responses

May 30 2012 01:33

Awesome thanks for sharing I was looking for a code snippet like this today and discovered this which worked perfectly.

Jun 05 2012 12:33

For non-tech people like me, could you supply and example what this hack would look like? Thanks.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox