Site icon CodeRevolution Knowledge Base

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

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!

Exit mobile version