I created a custom post type called "capitulo" and custom taxonomy called "serie" and i wanna output asigned taxonomy to custom post with link.
Here is my code
<?php //Do something if a specific array value exists within a post
$term_list = wp_get_post_terms($post->ID, 'serie', array("fields" => "all"));
$terms = get_terms( 'serie' );
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
// Then you can run a foreach loop to show the taxonomy terms infront.
foreach ( $term_list as $term_single ) {
echo '<h2><a href="' . esc_url( $term_link ) . '">' . $term_single->name . '</a></h2>';
}
}
?>
it output a link to all taxonomy terms with link for all custom post. Look
I created a custom post type called "capitulo" and custom taxonomy called "serie" and i wanna output asigned taxonomy to custom post with link.
Here is my code
<?php //Do something if a specific array value exists within a post
$term_list = wp_get_post_terms($post->ID, 'serie', array("fields" => "all"));
$terms = get_terms( 'serie' );
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
// Then you can run a foreach loop to show the taxonomy terms infront.
foreach ( $term_list as $term_single ) {
echo '<h2><a href="' . esc_url( $term_link ) . '">' . $term_single->name . '</a></h2>';
}
}
?>
it output a link to all taxonomy terms with link for all custom post. Look
Share Improve this question edited May 15, 2019 at 11:35 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked May 15, 2019 at 10:54 Marcelo HerreraMarcelo Herrera 32 bronze badges 1 |1 Answer
Reset to default 0Here’s the line that causes your problem:
$terms = get_terms( 'serie' );
You use get_terms
function which retrieves the terms in a given taxonomy.
And you want to get terms assigned to given post, so you should use get_the_terms
which retrieves the terms of the taxonomy that are attached to the post.
So change the line above to:
$terms = get_the_terms( get_the_ID(), 'serie' );
echo get_the_term_list( $post->ID, 'serie', '<h2>', '</h2><h2>', '</h2>' );
? You can find more aboutget_the_term_list()
in documentation. – nmr Commented May 15, 2019 at 11:18