1

I'd like to add a category class to body_class for custom post type to change body background color. I tried to rework the following code to work with custom post types, but no dice. It works with posts, but not the custom post type.

add_filter('body_class','add_category_to_single');
  function add_category_to_single($classes) {
    if (is_singular() ) {
      global $post;
      foreach((get_the_category($post->ID)) as $category) {
        // add category slug to the $classes array
        $classes[] = $category->category_nicename;
      }
    }
    // return the $classes array
    return $classes;
  }

I'm using Pods plugin and the latest WP.

2 Answers 2

2

your code only works if you are using category taxonomy. enter image description here

if you are using custom taxonomy like brands you must change your code to this:

function add_category_to_single( $classes ) {
    if ( is_singular() ) {
        global $post;

        $taxonomy = 'brands';
        $terms = get_the_terms( $post->ID, $taxonomy );

        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
            foreach ( $terms as $term ) {
                $classes[] = $term->slug;
            }
        }
    }

    return $classes;
}

add_filter( 'body_class', 'add_category_to_single' );

enter image description here

1
  • Agreed. @Single_pete: What is the taxonomy you registered to this custom post type?
    – Jeff Cohan
    Commented Sep 20 at 16:16
1

Wordpress as designed will add the class of the post type that you create to the body class list.

As an example if your post type is wspe_cats, wordpress will inject single-wspe-cats to the body of the single post for your CPT.

Similarly it will do the same for that CPT archive.

Let's say I create a taxonomy for my cpt called wspe-breeds, and set 3 terms (black, white, calico).

If I have an archive post for wspe_cats, WordPress will automatically add archive tax-wspe-breeds to the body.

If I went to the archive for my custom post taxonomy to look at calico cats, WordPress will automatically add archive tax-wspe-breeds to the body along with the these additional classes term-calico term-3 to the body of that archive. (Three came from the fact that that is the term's ID.)

So, if I'm reading your question correctly, there is no code that you need to create...WordPress did the heavy lifting!

3
  • 1
    I'm not sure if this is different for Pods plugin and the theme I'm using, but a single post of custom post type does not have a related custom taxonomy css class included in the body class. Commented Aug 11 at 22:19
  • I just installed PODS and tried it out (never used it before). It worked exactly as I expected and described above...so must be your theme. What theme are you using?
    – rudtek
    Commented Aug 12 at 18:54
  • 1
    I'm re-reading your comment and see what you mean. you're right, single pages don't show the category by default in class.
    – rudtek
    Commented Aug 12 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.