Using WordPress ‘get_the_term_list()’ PHP function

The get_the_term_list() WordPress PHP function retrieves a post’s terms as a list with a specified format. Terms are linked to their respective term listing pages.

Usage

echo get_the_term_list($post_id, $taxonomy, $before, $sep, $after);

Parameters

  • $post_id (int) – Required. The post ID.
  • $taxonomy (string) – Required. The taxonomy name.
  • $before (string) – Optional. String to use before the terms. Default: ”.
  • $sep (string) – Optional. String to use between the terms. Default: ”.
  • $after (string) – Optional. String to use after the terms. Default: ”.

More information

See WordPress Developer Resources: get_the_term_list()

Examples

Displaying a comma-separated term list

This example displays the job titles for a specific post as a comma-separated list.

echo get_the_term_list($post->ID, 'job_titles', '', ', ');

This example displays the job titles for a specific post as a comma-separated list without links.

echo wp_strip_all_tags(get_the_term_list($post->ID, 'job_titles', '', ', '));

Displaying a term list with custom prefix

This example displays the people associated with a specific post with a custom prefix.

echo get_the_term_list($post->ID, 'people', 'People: ', ', ');

Displaying a term list as an HTML list

This example displays the styles associated with a specific post as an HTML list.

echo get_the_term_list($post->ID, 'styles', '<ul class="styles"><li>', ',</li><li>', '</li></ul>');

Displaying a term list with custom separator

This example displays the categories associated with a specific post with a custom separator.

echo get_the_term_list($post->ID, 'category', '', ' | ', '');