Skip to content

Instantly share code, notes, and snippets.

@remkus
Created October 8, 2012 09:51
Show Gist options
  • Select an option

  • Save remkus/3851741 to your computer and use it in GitHub Desktop.

Select an option

Save remkus/3851741 to your computer and use it in GitHub Desktop.
Function to add category nicenames to body class
<?php
add_filter( 'body_class', 'tax_body_class' );
/**
* This function adds category nicenames to body class.
*
* @access public
* @author Remkus de Vries
* @param mixed $classes
* @return void
*/
function tax_body_class( $classes ) {
if ( is_singular('post') || is_category() ) {
global $post;
$terms = get_the_terms( $post->ID, 'category' );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
// assign body class for the blog categories
$classes[] =$term->slug;
}
}
}
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment