the_terms()
Display the terms in a list.
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
Return
null|false
. Void on success, false on failure.
Usage
the_terms( $post_id, $taxonomy, $before, $sep, $after );
- $post_id(int) (required)
- Post ID.
- $taxonomy(string) (required)
- Taxonomy name.
- $before(string)
- String to use before the terms.
Default: '' - $sep(string)
- String to use between the terms.
Default: ', ' - $after(string)
- String to use after the terms.
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.8
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 ); }