Output terms to post_class()

I am trying to output all terms attached to a specific post (including custom taxonomy terms) as CSS classes applied to a DIV
Something like that :

   <div <?php post_class('class-name'); ?>></div>

So it outputs something like this :

Read More
  <div class="class-name term1 term2 term3 term4"></div>

How do I do this?
Thanks!

Related posts

1 comment

  1. The easiest and most straight-forward way to do this would be:

    $tax_terms = get_the_terms(
      $post->ID,
      array('genre')
    );
    $tax_terms = wp_list_pluck($tax_terms,'slug');
    post_class(implode(' ',$tax_terms)); 
    

    You could also apply a filter to post_class that does essentially the same.

    function tax_classes_wpse_105386($classes) {
      global $post;
      $tax_terms = get_the_terms(
    $post->ID,
    array('genre')
      );
      $tax_terms = wp_list_pluck($tax_terms,'slug');
      $classes = array_merge($classes,$tax_terms);
      return $classes;
    }
    add_filter('post_class','tax_classes_wpse_105386');
    

    I am sure you want more complicated logic though– for example, restrict this only to certain post types perhaps.

Comments are closed.