How to add tags (custom taxonomy) to post class css?

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.

Read More
<?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; ?>

Thanks.

Related posts

Leave a Reply

1 comment

  1. 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;  
    }  
     ?>">