Server: 11:42 AM
Reset: 12:00 AM
  johnny Admin Pioneer Moderator
Male Avatar
Received: $5.31
Balance: $5.190
Referrals: 16
Posts: 93
Member Since: 04-27-2018
PM User
Achievements Completed
1 Cashout 10 Direct Referrals Valued Advertiser

How to add current custom taxonomy slug to body class
Let's assume your theme uses custom taxonomy called "B section" and there are lots of sections set up (instead of categories). There is also a custom post type and the posts are assigned to sections.

And you need to style the header differently depending on what section(s) the post belongs to. So that the posts that belong to Section A have got a different header background than the ones belonging to Section B etc. If a post belongs to more than one section it doesn't matter which one it picks. It can always be the first one in the array.

Then you can use this code to add a taxonomy luck class in your body tag.

function add_taxonomy_to_single( $classes ) {
if ( is_single() ) {
global $post;
$my_terms [add eaqual sign here! ] get_the_terms( $post-ID, 'custom-taxonomy' );
if ( $my_terms && ! is_wp_error( $my_terms ) ) {
return $classes;
add_filter( 'body_class', 'add_taxonomy_to_single' );

Thanks feel free to add yours in the comment section

Jan 14th 2019 at 5:32 PM
  johnny Admin Pioneer Moderator
Male Avatar

this works 100% for me but if there is a better way to do it let me know

Jan 17th 2019 at 9:10 AM
Page 1 of 11