最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Showing taxonomy terms on custom post type

programmeradmin3浏览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.

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.

Share Improve this question asked Nov 15, 2019 at 2:59 user12204773user12204773 1
Add a comment  | 

1 Answer 1

Reset to default 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)
  );
}
发布评论

评论列表(0)

  1. 暂无评论