get_the_term_list

Retrieve a post's terms as a list with specified format.

Description

(string|false|WP_Error) get_the_term_list( (int) $id, (string) $taxonomy, (string) $before = '', (string) $sep = '', (string) $after = '' ); 

Returns (string|false|WP_Error)

A list of terms on success, false if there are no terms, WP_Error on failure.

Parameters (5)

0. $id (int)
Post ID.
1. $taxonomy (string)
Taxonomy name.
2. $before — Optional. (string) => ''
Before list.
3. $sep — Optional. (string) => ''
Separate items using this.
4. $after — Optional. (string) => ''
After list.

Usage

  1. if ( !function_exists( 'get_the_term_list' ) ) { 
  2. require_once ABSPATH . WPINC . '/category-template.php'; 
  3.  
  4. // Post ID. 
  5. $id = -1; 
  6.  
  7. // Taxonomy name. 
  8. $taxonomy = ''; 
  9.  
  10. // Optional. Before list. 
  11. $before = ''; 
  12.  
  13. // Optional. Separate items using this. 
  14. $sep = ''; 
  15.  
  16. // Optional. After list. 
  17. $after = ''; 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = get_the_term_list($id, $taxonomy, $before, $sep, $after); 
  21.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/category-template.php  
  1. function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) { 
  2. $terms = get_the_terms( $id, $taxonomy ); 
  3.  
  4. if ( is_wp_error( $terms ) ) 
  5. return $terms; 
  6.  
  7. if ( empty( $terms ) ) 
  8. return false; 
  9.  
  10. $links = array(); 
  11.  
  12. foreach ( $terms as $term ) { 
  13. $link = get_term_link( $term, $taxonomy ); 
  14. if ( is_wp_error( $link ) ) { 
  15. return $link; 
  16. $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>'; 
  17.  
  18. /** 
  19. * Filters the term links for a given taxonomy. 
  20. * The dynamic portion of the filter name, `$taxonomy`, refers 
  21. * to the taxonomy slug. 
  22. * @since 2.5.0 
  23. * @param array $links An array of term links. 
  24. */ 
  25. $term_links = apply_filters( "term_links-{$taxonomy}", $links ); 
  26.  
  27. return $before . join( $sep, $term_links ) . $after;