sinanisler logo

add custom taxonomy to the custom post type single body class

add this to the functions.php

edit it to your needs

// Add Taxonomy to Post Type
function my_custom_taxonomy() {
    register_taxonomy(
        'status',
        'post_type_name_single',
        array(
            'label' => 'status',
            'rewrite' => array( 'slug' => 'status' ),
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'my_custom_taxonomy' );

// Add Taxonomy to Body Class
function my_body_classes( $classes ) {
    global $post;
    if ( is_singular( 'post_type_name_single' ) ) {
        $terms = get_the_terms( $post->ID, 'status' );
        if ( $terms && ! is_wp_error( $terms ) ) {
            foreach ( $terms as $term ) {
                $classes[] = 'status-' . $term->slug;
            }
        }
    }
    return $classes;
}
add_filter( 'body_class', 'my_body_classes' );


 

Leave the first comment