As of WordPress 4.2-alpha-31271 (23 January 2015), CSS classes for custom taxonomy terms are now automatically added by WordPress core when using post_class(). However, they are not automatically attached to the body classes.
You can use the following utility function to add your custom taxonomies to the body class for use as CSS hooks in themes:
/** * Add Custom Taxonomy Terms To The Body Class */ add_filter( 'body_class', 'atp_custom_taxonomy_body_class', 10, 3 ); if ( ! function_exists('atp_custom_taxonomy_body_class') ) { function atp_custom_taxonomy_body_class($classes, $class, $ID) { $taxonomies_args = array( 'public' => true, '_builtin' => false, ); $taxonomies = get_taxonomies( $taxonomies_args, 'names', 'and' ); $terms = get_the_terms( (int) $ID, (array) $taxonomies ); if ( ! empty( $terms ) ) { foreach ( (array) $terms as $order => $term ) { if ( ! in_array( $term->slug, $classes ) ) { $classes[] = $term->slug; } } } $classes[] = 'clearfix'; return $classes; } }
Put the above code in your theme’s functions.php file. Then, whenever the body_class() is used in a template:
<body <?php body_class();?> >
It will output any public custom taxonomy term(s) attached to the post, in addition to all of the default body classes.
Recent Comments