irislines web design
irislines web design

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:

http://wpcodesnippet.com/add-post-category-slug-body-class-wordpress/

 

 

IrisLines LLC
101 Washington Street
Brattleboro, VT 05301

802 257 7391

info@irislines.com