transmitstudio
11/9/2014 - 1:23 AM

Add category slug as body class

Add category slug as body class

<?php
// to the body element, add category slug as a class so we can style posts by category
add_filter('body_class','add_category_to_single');
function add_category_to_single($classes) {
	if (is_single() ) {
		global $post;
		foreach((get_the_category($post->ID)) as $category) {
			// add category slug to the $classes array
			$classes[] = $category->category_nicename;
		}
	}
	// return the $classes array
	return $classes;
}
?>