This page redirects to an external site: https://developer.wordpress.org/reference/functions/get_the_terms/
Languages: English • the terms 日本語 (Add your language)
Retrieve the terms of the taxonomy that are attached to the post.
<code style="color: #000000"> <span style="color: #0000BB"><?php get_the_terms</span><span style="color: #007700">( </span><span style="color: #0000BB">$id</span><span style="color: #007700">, </span><span style="color: #0000BB">$taxonomy </span><span style="color: #007700">); </span><span style="color: #0000BB">?></span> </code>
false if the post contains no terms from the given taxonomy; false if the post doesn't exist; and a Class_Reference/WP_Error object if an invalid taxonomy is entered.A post with no terms assigned gives a false result, not an empty array.
Each term object will contain the following fields:
stdClass Object
(
[term_id] =>
[name] =>
[slug] =>
[term_group] =>
[term_order] =>
[term_taxonomy_id] =>
[taxonomy] =>
[description] =>
[parent] =>
[count] =>
[object_id] =>
)
Echoing the list of terms (for a taxonomy called on-draught). This is similar to the output from get_the_term_list, but without the terms being hyperlinked:
<?php
$terms = get_the_terms( $post->ID, 'on-draught' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
$on_draught = join( ", ", $draught_links );
?>
<p class="beers draught">
On draught: <span><?php echo $on_draught; ?></span>
</p>
<?php endif; ?>
place this function in theme functions.php
<?php
// get taxonomies terms links
function custom_taxonomies_terms_links(){
// get post by post id
$post = get_post( $post->ID );
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( !empty( $terms ) ) {
$out[] = "<h2>" . $taxonomy->label . "</h2>\n<ul>";
foreach ( $terms as $term ) {
$out[] =
' <li><a href="'
. get_term_link( $term->slug, $taxonomy_slug ) .'">'
. $term->name
. "</a></li>\n";
}
$out[] = "</ul>\n";
}
}
return implode('', $out );
}
?>
Now you can use this function in your themes for your post types without the need to input anything
<?php echo custom_taxonomies_terms_links(); ?>
It uses get_object_term_cache() or wp_get_object_terms() to retrieve results.
get_the_terms() is located in wp-includes/category-template.php.