Server: 07:49 PM
Reset: 11:00 PM
426
31881
76.87
  johnny Admin Pioneer Moderator
Male Avatar
Received: $5.31
Balance: $5.097
Referrals: 6
Posts: 72
Member Since: 04-27-2018
PM User
Achievements Completed
1 Cashout 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 ) ) {
$classes[]
}
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
Options
  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
Options
Page 1 of 11