Viewing 3 replies - 1 through 3 (of 3 total)
  • I’m pretty sure this is what you need. I got this working to show both the category & taxonomy class on a single post:

    // add css class to body on single post.
    function add_category_to_single($classes) {
            if (is_single() ) {
                    global $post;
                    foreach((get_the_category($post->ID)) as $category) {
                            $classes[] = 'category-' . $category->category_nicename;
                    }
            }
    
            $terms = wp_get_object_terms($post->ID, 'add-your-taxonomy-here');
            $count = count($terms);
            if ( $count > 0 ){
                    foreach ( $terms as $term ) {
                            $classes[] =  "taxonomy-" . $term->name;
                    }
            }
    
            return $classes;
    
    }
    add_filter('body_class','add_category_to_single');

    I would love it if anyone could help me with this.

    vitamincee I tried your code and this is what I get in the body tag

    <body class="single single-bespoke postid-11 taxonomy- taxonomy-">

    i.e. it says taxonomy-
    but doesn’t add the actual taxonomy term in.

    Can you help?

    This worked for me. Remember to change “your_taxonomy_name_here” to your actual taxonomy name.

    // add taxoonomy term to body_class
    function wpprogrammer_custom_taxonomy_in_body_class( $classes ){
      if( is_singular() )
      {
        $custom_terms = get_the_terms(0, 'your_taxonomy_name_here');
        if ($custom_terms) {
          foreach ($custom_terms as $custom_term) {
            $classes[] = 'custom-tax-' . $custom_term->slug;
          }
        }
      }
      return $classes;
    }
    
    add_filter( 'body_class', 'wpprogrammer_custom_taxonomy_in_body_class' );
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Style Taxonomy Posts?’ is closed to new replies.