I was surprised to learn that custom taxonomies aren't added as body or post classes like categories and tags are.

I'm sure this will be added in a future version of WordPress, but in the meantime I need to add a custom taxonomy to the post class so that I can style post in a certain category in that taxonomy differently.

It'd be most elegant to filter the post class and add the taxonomies to it. I found a snippet to pull off a similar trick with the body class, but I haven't been successful in adapting it:

    function wpprogrammer_post_name_in_body_class( $classes ){
 if( is_singular() )
 {
  global $post;
  array_push( $classes, "{$post->post_type}-{$post->post_name}" );
 }
 return $classes;
}

add_filter( 'body_class', 'wpprogrammer_post_name_in_body_class' );

A bit more crudely, I thought about using the_terms function to create my own classes for the custom posts, something like this:

<div class="<?php the_terms( $post->ID, 'taxonomy', '', ' ', '' ); ?>"></div>

But then I'd have to filter out the HTML that the_term generates.

Am I missing anything obvious here, is there a simpler way to solve this issue?

link|improve this question
feedback

2 Answers

up vote 2 down vote accepted

I found a snippet of code courtesy of mfields that solved this problem for me, here's what I ended up using:

<?php   // Add custom taxonomies to the post class

    add_filter( 'post_class', 'custom_taxonomy_post_class', 10, 3 );

    if( !function_exists( 'custom_taxonomy_post_class' ) ) {

        function custom_taxonomy_post_class( $classes, $class, $ID ) {

            $taxonomy = 'listing-category';

            $terms = get_the_terms( (int) $ID, $taxonomy );

            if( !empty( $terms ) ) {

                foreach( (array) $terms as $order => $term ) {

                    if( !in_array( $term->slug, $classes ) ) {

                        $classes[] = $term->slug;

                    }

                }

            }

            return $classes;

        }

    }  ?>
link|improve this answer
feedback

Instead of the_terms, you could use get_the_terms, which will return the taxonomy term objects. The Codex has documentation for the tag-only version, get_the_tags.

That would give you something like this:

function wpprogrammer_custom_taxonomy_in_body_class( $classes ){
  if( is_singular() )
  {
    $custom_terms = get_the_terms(0, 'my_custom_taxonomy');
    if ($custom_terms) {
      foreach ($custom_terms as $custom_term) {
        $classes[] = 'custom_tag_' . $custom_term->slug;
      }
    }
  }
  return $classes;
}

add_filter( 'body_class', 'wpprogrammer_custom_taxonomy_in_body_class' );
link|improve this answer
Thanks Jan for pointing me in the right direction! I hand't tried get_the_terms or get_the_tags, get_the_tags didn't quite work for me, but I did find a solution with get_the_terms that worked. – JLeuze Sep 27 '10 at 18:56
@JLeuze: Indeed, my code was incorrect, it had get_the_tags where it needed to be get_the_terms, the more generic version - I'll correct it. But the Codex only explains get_the_tags, that's why I included it. – Jan Fabry Sep 27 '10 at 19:32
feedback

Your Answer

 
or
required, but never shown

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