WordPress the_terms() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Template tag

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.

WordPress
the_terms( get_the_ID(), 'category' );
the_terms( get_the_ID(), 'genre', 'Genres: ', ', ', '' );
CallWhat 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
WordPress
the_terms( int $post_id, string $taxonomy, string $before = '', string $sep = ', ', string $after = '' ): false|void

Parameters

ParameterDescription
$post_idRequired. The post ID. Use get_the_ID() inside The Loop.
$taxonomyRequired. The taxonomy name, such as category, post_tag, or a custom name.
$beforeOptional. Text placed before the list. Default is an empty string.
$sepOptional. Text placed between each term. Default is ,.
$afterOptional. Text placed after the list. Default is an empty string.

Return Value

Return ValueDescription
voidThe terms are printed. Nothing is returned.
falseWordPress 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.

WordPress
<?php the_terms( get_the_ID(), 'category', 'Posted in: ', ', ', '' ); ?>

How It Works

  1. get_the_ID() gives the current post ID.
  2. 'category' is the taxonomy name.
  3. The label and comma are printed with the links.

Example 2 — Custom taxonomy

Print the terms of a custom taxonomy.

WordPress
<p class="genres">
  <?php the_terms( get_the_ID(), 'genre', 'Genres: ', ', ', '' ); ?>
</p>

How It Works

  1. genre is a custom taxonomy you registered.
  2. The links are printed inside your own paragraph.
  3. If there are no terms, the paragraph stays empty.

Example 3 — Tags in a list

Print tags as list items.

WordPress
<ul class="tag-list">
  <?php the_terms( get_the_ID(), 'post_tag', '<li>', '</li><li>', '</li>' ); ?>
</ul>

How It Works

  1. post_tag is the tag taxonomy.
  2. The separator closes one li and opens the next.
  3. 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().

Next: wp_dropdown_categories()

Create an HTML select box (drop-down) of categories.

wp_dropdown_categories() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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