zartgesotten
12/14/2017 - 10:15 AM

Add custom taxonomy terms to body class

<?php
/**
* 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;
                }
            }
        }
?>