WordPress get_the_term_list() Function

Definition
In WordPress, get_the_term_list() returns the terms of a post for one taxonomy. Each term is a link to its archive page.
It returns an HTML string. It does not print it. If you want to print the list straight away, use the_terms().
Syntax
Pass the post ID and the taxonomy name. The text before, between, and after is optional.
get_the_term_list( get_the_ID(), 'category' );
get_the_term_list( get_the_ID(), 'post_tag', 'Tags: ', ', ', '' );
| Call | What it does |
|---|
get_the_term_list( $post_id, 'category' ) | Returns the categories of a post as links. |
get_the_term_list( $post_id, 'post_tag', 'Tags: ', ', ' ) | Adds text before the list and a comma between links. |
get_the_term_list( $post_id, 'genre' ) | Works with a custom taxonomy such as genre. |
Always test the result. It can be false or a WP_Error object.
Full declaration
get_the_term_list( int $post_id, string $taxonomy, string $before = '', string $sep = '', string $after = '' ): string|false|WP_Error
Parameters
| Parameter | Description |
|---|
$post_id | Required. The post ID. Use get_the_ID() inside The Loop. |
$taxonomy | Required. The taxonomy name, such as category, post_tag, or a custom name. |
$before | Optional. Text placed before the list. Default is an empty string. |
$sep | Optional. Text placed between each term. Default is an empty string. |
$after | Optional. Text placed after the list. Default is an empty string. |
Return Value
| Return Value | Description |
|---|
| string | HTML links to the terms, joined by the separator. |
| false | The post has no terms in this taxonomy. |
| WP_Error | The taxonomy does not exist. |
WordPress Examples
Three short theme examples. Each one has a sample output.
Example 1 — Categories as links
Get the categories of the current post.
<?php
$list = get_the_term_list( get_the_ID(), 'category', '', ', ', '' );
echo $list;
?>
<a href="https://example.com/category/news/" rel="tag">News</a>, <a href="https://example.com/category/tips/" rel="tag">Tips</a>
How It Works
get_the_ID() gives the current post ID.'category' is the taxonomy name.- The comma is placed between the links.
Example 2 — Custom taxonomy with a label
Add a label and wrap the result in a paragraph.
<?php
$genres = get_the_term_list( get_the_ID(), 'genre', 'Genres: ', ', ', '' );
if ( $genres ) {
echo '<p>' . $genres . '</p>';
}
?>
<p>Genres: <a href="https://example.com/genre/drama/" rel="tag">Drama</a>, <a href="https://example.com/genre/comedy/" rel="tag">Comedy</a></p>
How It Works
genre is a custom taxonomy you registered.Genres: is printed before the links.- The
if stops an empty paragraph when there are no terms.
Example 3 — Check false and WP_Error
Test the result before you print it.
<?php
$tags = get_the_term_list( get_the_ID(), 'post_tag', 'Tags: ', ', ', '' );
if ( $tags && ! is_wp_error( $tags ) ) {
echo '<p class="tags">' . $tags . '</p>';
}
?>
<p class="tags">Tags: <a href="https://example.com/tag/php/" rel="tag">PHP</a>, <a href="https://example.com/tag/wordpress/" rel="tag">WordPress</a></p>
How It Works
$tags is true only when links were returned.is_wp_error() catches a wrong taxonomy name.- The paragraph is printed only when the result is safe to use.
Frequently Asked Questions
It returns an HTML string of linked terms. It can also return false when there are no terms, or a WP_Error when the taxonomy does not exist.
get_the_term_list() returns the string. the_terms() prints it.
Yes. Pass the taxonomy name as the second argument, for example 'genre'.
You only need a post ID. Inside The Loop, use get_the_ID(). Outside The Loop, pass any post ID.
Use get_the_terms() and loop over the term objects. Then read each name with $term->name.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful