1

I need to get ONLY the term (not all terms) of Current Custom Post Type. For example I have a Custom Post type called movies and have a Taxonomy called Genre which has some terms like comedy, action,... now in current post I need to get the used term?

$args = array( 'post_type' => 'movies', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
  the_title();
  echo '<div class="entry-content">';
  the_content();
  echo '</div>';
endwhile;

1 Answer 1

2

There are several ways to achieve this.

Using get_the_terms:

$args = array( 'post_type' => 'movies', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php
    the_title();

    $terms = get_the_terms( get_the_ID(), 'genre' );
    if ( is_array( $terms ) ) {
        //Manipulate array of WP_Term objects
    }
    ?>
    <div class="entry-content">
    <?php the_content(); ?>
    </div>
<?php endwhile; ?>

using get_the_term_list:

$args = array( 'post_type' => 'movies', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php
    the_title();

    echo get_the_term_list( get_the_ID(), 'genre' );
    ?>
    <div class="entry-content">
        <?php the_content(); ?>
    </div>
<?php endwhile; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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