Category as body class in WordPress
Wanting more handles for CSS styles — to style a widget in different colors for different categories of posts — I found this snippet:
// add post category slug to body class in wordpress
function wcs_add_category_slug_body_class( $classes ) {
global $post;
if ( is_single() ) {
foreach ( get_the_category( $post->ID ) as $category ) {
$classes[] = $category->category_nicename;
}
}
return $classes;
}
add_filter( 'body_class', 'wcs_add_category_slug_body_class' );;
which did the trick!
The site that had the snippet: