What is the purpose of body_class function in wordpress ?
body_class function helps to show the bunch of classes name to the body element that have information about what kind of page is currently being displayed.
Here is the way to implement body_class function into theme template.
<body <?php body_class(); ?>>
So now we want to add category name to body class .. mostly for styling purposes. 😉
Add this code to your functions.php file and hit on save button.
add_filter('body_class','nst_add_category_name');
function nst_add_category_name($classes) {
if (is_single() ) {
global $post;
foreach((get_the_category($post->ID)) as $category) {
// add category slug name to the $classes array
$classes[] = $category->category_nicename;
}
}
// return the $classes array here
return $classes;
}
I hope this helps you.. Thanks 🙂