WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to figure out how to add my custom taxonomy for tags to the post class css when I already have added the custom taxonomy for categories. The code Im currently using to add the category taxonomy is -

  <?php $terms = get_the_terms( $post->ID, 'videoscategory' ); ?>
  <div class="box<?php foreach( $terms as $term ) echo ' ' . $term->slug; ?>">

This works fine but I also need to add a class for tags I've created with a custom taxonomy. I came across this code that will add the regular tags, but I need to use my custom tags.

<?php $tags = get_the_tags();
if( $tags ) : ?>
  <?php foreach( $tags as $tag ) { ?>
   <span class="<?php echo $tag->slug; ?>"><a href="<?php echo get_tag_link($tag->term_id); ?>"><?php echo $tag->name; ?></a></span>
  <?php } ?>
<?php endif; ?>


share|improve this question
Does any one have an idea? – mygm26 Dec 23 '12 at 4:33
I think I found a possible solution but the problem is Im getting all the tags instead of just one that is attached to the post. – mygm26 Dec 23 '12 at 4:58
up vote 0 down vote accepted

Ok I figured out how to add the terms to div's class -

In the code I have first the category, then the post type, then a custom taxonomy tag term assigned to it -

<?php $terms = get_the_terms( $post->ID, 'YOUR CUSTOM TAXONOMY CAT' ); ?>
 <?php $post_type = get_post_type($post->ID); ?>
<div class="box<?php foreach( $terms as $term ) echo ' ' . $term->slug; ?><?php echo ' '.get_post_type( $post->ID ); ?> <?php  $terms = wp_get_post_terms($post->ID,'YOUR CUSTOM TAXONOMY TAGS');  
foreach ($terms as $term) {  
    echo $term->slug;  
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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