Created
June 4, 2014 00:55
-
-
Save jayseventwo/ad270505f4f6bae125e6 to your computer and use it in GitHub Desktop.
Add category class to body in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add category class to body | |
function body_class_add_categories( $classes ) { | |
// Only proceed if we're on a single post page | |
if ( !is_single() ) | |
return $classes; | |
// Get the categories that are assigned to this post | |
$post_categories = get_the_category(); | |
// Loop over each category in the $categories array | |
foreach( $post_categories as $current_category ) { | |
// Add the current category's slug to the $body_classes array | |
$classes[] = 'category-' . $current_category->slug; | |
} | |
// Finally, return the $body_classes array | |
return $classes; | |
} | |
add_filter( 'body_class', 'body_class_add_categories' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment