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

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


Description Description


Parameters Parameters

$id

(int) (Required) Post ID.

$taxonomy

(string) (Required) Taxonomy name.

$before

(string) (Optional) Before list.

Default value: ''

$sep

(string) (Optional) Separate items using this.

Default value: ''

$after

(string) (Optional) After list.

Default value: ''


Top ↑

Return Return

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


Top ↑

Source Source

File: wp-includes/category-template.php

function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
	$terms = get_the_terms( $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 filter name, `$taxonomy`, refers
	 * to the taxonomy slug.
	 *
	 * @since 2.5.0
	 *
	 * @param array $links An array of term links.
	 */
	$term_links = apply_filters( "term_links-{$taxonomy}", $links );

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

Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

    Returning an HTML List

    Used inside the loop this outputs the terms from the styles taxonomy for a specific post as an (x)html list.

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

    This would return something like.

    <ul class="styles">
        <li><a href="person1">Style 1</a>,</li> 
        <li><a href="person2">Style 2</a></li>
    </ul>
    

You must log in before being able to contribute a note or feedback.