get_the_term_list() WordPress Function

The get_the_term_list() function is a built-in WordPress function that allows you to retrieve a list of terms associated with a post. This function can be used in conjunction with the get_the_ID() function to get a list of terms for a specific post.

get_the_term_list( int $post_id, string $taxonomy, string $before = '', string $sep = '', string $after = '' ) #

Retrieves a post’s terms as a list with specified format.


Description

Terms are linked to their respective term listing pages.


Top ↑

Parameters

$post_id

(int)(Required)Post ID.

$taxonomy

(string)(Required)Taxonomy name.

$before

(string)(Optional) String to use before the terms.

Default value: ''

$sep

(string)(Optional) String to use between the terms.

Default value: ''

$after

(string)(Optional) String to use after the terms.

Default value: ''


Top ↑

Return

(string|false|WP_Error) A list of terms on success, false if there are no terms, WP_Error on failure.


Top ↑

Source

File: wp-includes/category-template.php

function get_the_term_list( $post_id, $taxonomy, $before = '', $sep = '', $after = '' ) {
	$terms = get_the_terms( $post_id, $taxonomy );

	if ( is_wp_error( $terms ) ) {
		return $terms;
	}

	if ( empty( $terms ) ) {
		return false;
	}

	$links = array();

	foreach ( $terms as $term ) {
		$link = get_term_link( $term, $taxonomy );
		if ( is_wp_error( $link ) ) {
			return $link;
		}
		$links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
	}

	/**
	 * Filters the term links for a given taxonomy.
	 *
	 * The dynamic portion of the hook name, `$taxonomy`, refers
	 * to the taxonomy slug.
	 *
	 * Possible hook names include:
	 *
	 *  - `term_links-category`
	 *  - `term_links-post_tag`
	 *  - `term_links-post_format`
	 *
	 * @since 2.5.0
	 *
	 * @param string[] $links An array of term links.
	 */
	$term_links = apply_filters( "term_links-{$taxonomy}", $links );  // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores

	return $before . implode( $sep, $term_links ) . $after;
}


Top ↑

Changelog

Changelog
VersionDescription
2.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.