WordPress the_terms() Function

Definition
In WordPress, the_terms() prints the terms of a post for one taxonomy. Each term is a link to its archive page.
It prints the links. It does not return them. It prints the result of get_the_term_list().
Syntax
Pass the post ID and the taxonomy name. The text before, between, and after is optional.
the_terms( get_the_ID(), 'category' );
the_terms( get_the_ID(), 'genre', 'Genres: ', ', ', '' );
| Call | What it does |
|---|
the_terms( $post_id, 'category' ) | Prints the categories of the post as links. |
the_terms( $post_id, 'genre', 'Genres: ', ', ' ) | Prints a custom taxonomy with a label and a comma. |
the_terms( $post_id, 'post_tag', '<p>', ' | ', '</p>' ) | Prints tags inside a paragraph. |
Use get_the_term_list() if you want the HTML as a string.
Full declaration
the_terms( int $post_id, string $taxonomy, string $before = '', string $sep = ', ', string $after = '' ): false|void
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 ,. |
$after | Optional. Text placed after the list. Default is an empty string. |
Return Value
| Return Value | Description |
|---|
| void | The terms are printed. Nothing is returned. |
| false | WordPress found an error, for example the taxonomy does not exist. |
WordPress Examples
Three short theme examples. Each one has a sample output.
Example 1 — Categories
Print the categories of the current post.
<?php the_terms( get_the_ID(), 'category', 'Posted in: ', ', ', '' ); ?>
Posted in: <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 label and comma are printed with the links.
Example 2 — Custom taxonomy
Print the terms of a custom taxonomy.
<p class="genres">
<?php the_terms( get_the_ID(), 'genre', 'Genres: ', ', ', '' ); ?>
</p>
<p class="genres">
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.- The links are printed inside your own paragraph.
- If there are no terms, the paragraph stays empty.
Example 3 — Tags in a list
Print tags as list items.
<ul class="tag-list">
<?php the_terms( get_the_ID(), 'post_tag', '<li>', '</li><li>', '</li>' ); ?>
</ul>
How It Works
post_tag is the tag taxonomy.- The separator closes one
li and opens the next. - The before and after text open the first and close the last item.
Frequently Asked Questions
It prints the terms of a post for one taxonomy as links.
the_terms() prints the list. get_the_term_list() returns it as a string.
Yes. Use the taxonomy names category and post_tag. It also works with custom taxonomies.
It prints the links and returns nothing. It returns false only when WordPress finds an error, such as a missing taxonomy.
Yes, it is required. Inside The Loop use get_the_ID().
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