the_terms()
Outputs a list of links to terms (taxonomy elements) related to the specified post.
Uses: get_the_term_list()
1 time — 0.00357 sec (very slow) | 50000 times — 5.11 sec (fast) | PHP 7.1.2, WP 4.7.3
Hooks from the function
Returns
null|false. Displays a list of links to the terms to which the object (post) is attached.
Usage
<?php the_terms( $id, $taxonomy, $before, $sep, $after ); ?>
- $id(integer) (required)
- ID of the post, the taxonomy elements of which need to be retrieved.
- $taxonomy(string) (required)
- Name of the taxonomy, the elements of which need to be retrieved.
- $beforestring
- Text that will be shown before the list.
Default: '' - $sepstring
- Separator text that will be shown between the output elements (tags).
Default: ', ' - $afterstring
- Closing text (text at the end of the list).
Default: ''
Examples
#1 Display a list of elements of the planets taxonomy
Suppose we have an custom taxonomy planets and a post with ID 67, which refers to 2 elements of this taxonomy: Earth, Mars. Then the code:
<?php the_terms( 67, 'planets', 'In categories: '); ?>
displays:
In categories: <a href="#">Earth</a>, <a href="#">Mars</a>
Changelog
| Since 2.5.0 | Introduced. |
the_terms() the terms code WP 6.9
function the_terms( $post_id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
$term_list = get_the_term_list( $post_id, $taxonomy, $before, $sep, $after );
if ( is_wp_error( $term_list ) ) {
return false;
}
/**
* Filters the list of terms to display.
*
* @since 2.9.0
*
* @param string $term_list List of terms to display.
* @param string $taxonomy The taxonomy name.
* @param string $before String to use before the terms.
* @param string $sep String to use between the terms.
* @param string $after String to use after the terms.
*/
echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after );
}