Codex

Function Reference/get the terms

Contents

Description

Retrieve the terms of the taxonomy that are attached to the post.

Usage

 <?php get_the_terms$id$taxonomy ); ?> 

Parameters

$id
(int) (required) Post ID
Default: 0
$taxonomy
(string|array) (required) Name of taxonomy to retrieve terms from. For example: 'category', 'post_tag', 'taxonomy slug'
Default: None

Returns

(array|false|wp_error) 
Array of term objects on success. False if no terms are found in the given taxonomy and a wp_error object if an invalid taxonomy is entered.

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] =>
)

Example

A Basic Example

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; ?>

Get terms for all custom taxonomies

place this function in theme functions.php

<?php // get taxonomies terms links
function custom_taxonomies_terms_links() {
	global $post, $post_id;
	// 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);
	foreach ($taxonomies as $taxonomy) {
		// get the terms related to post
		$terms = get_the_terms( $post->ID, $taxonomy );
		if ( !empty( $terms ) ) {
			$out = array();
			foreach ( $terms as $term )
				$out[] = '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a>';
			$return = join( ', ', $out );
		}
	}
	return $return;
} ?>

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(); ?>

Notes

It uses get_object_term_cache() or wp_get_object_terms() to retrieve results.

Change Log

Source File

get_the_terms() is located in wp-includes/category-template.php.

Related

See also index of Function Reference and index of Template Tags.