0

I have a custom post type named movies and taxonomy is genres.
Now there's a movie "random" with genres "horror" "drama" "adventure"

What I want to do is: list all of the associated genres on that movie page and when you click on a genre it links to that genre page. How can I do that.

1

You might have already figured this out, but here's an example how to get custom taxonomy terms for a post and create a list of links for the found terms.

// used in your custom post type template, e.g. single-movies.php
$genres = get_the_terms( get_the_ID(), 'genres' );
// current post has genre terms attached to it
if ( $genres && ! is_wp_error( $genres ) ) {
  $term_links = array();
  // build list item links
  foreach ($genres as $genre) {
    $genre_links[] = sprintf(
      '<li><a href="%s">%s</a></li>',
      esc_url(get_term_link($genre->term_id, $genre->taxonomy)),
      esc_html($genre->name)
    );
  }
  // display genres link list
  printf(
    '<ul>%s</ul>',
    implode('', $genre_links)
  );
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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