0

I have custom taxonomy (video-category) and custom post type (video). I want to show taxonomy link and name in my post items in WP_Query loop.

I use this to show that, But not working:

<?php get_the_terms( get_the_ID(), 'video-category', ', ' ) ?>

My WP_Query loop:

<?php
$i = 0;
$query = new WP_Query(
    array(
      'offset'=> 1,
      'posts_per_page'=>2,
      'post_type' => 'video',
    )
);

if( $query->have_posts() ) {
    while( $query->have_posts() ){ $query->the_post();
       $image = wp_get_attachment_image_src(
           get_post_thumbnail_id( $post->ID ), 'medium' ); ?>
           <div class="post post-thumb">
           <a class="post-img" href="<?php the_permalink() ?>">
               <img src="<?php echo $image[0]; ?>">
           </a>
           <div class="post-body">
               <div class="post-category">
                   <a href="<?php echo get_category_link(get_the_category()[0]->cat_ID); ?>"><?php get_the_terms( get_the_ID(), 'video-category', ', ' ) ?>
                   </a>
               </div>
               <h3 class="post-title">
                   <a href="<?php the_permalink() ?>"><?php the_title() ?></a>
               </h3>
               <ul class="post-meta">
                   <li><?php the_author(); ?></li>
                   <li><?php the_time( 'j F Y'); ?></li>
               </ul>
           </div>
           <div class="i5w0Le" aria-hidden="true">
               <span class="z1asCe UIgqBe">
                   <svg focusable="false" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 14.5v-9l6 4.5-6 4.5z"></path>
                   </svg>
               </span>
           </div>
       </div>
       <?php
       $i++;
    }
} ?>

1 Answer 1

0

Please try this

<div class="post-category">
<?php
// @ https://developer.wordpress.org/reference/functions/get_the_terms/
$terms = get_the_terms( get_the_ID(), 'video-category' );
if ( $terms && ! is_wp_error( $terms ) ){
    $term_links = array();
    foreach ( $terms as $term ) {
        $term_links[] = '<a href="' . esc_attr( get_term_link( $term->slug, $taxonomy ) ) . '">' . __( $term->name ) . '</a>';
    }
    $all_terms = join( ', ', $term_links );
    echo '<span class="terms-' . esc_attr( $term->slug ) . '">' . __( $all_terms ) . '</span>';
}
?>
</div>

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.