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 11 Answer
Reset to default 1You 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)
);
}