Paramètres

$post_id(int)requis

Id du poste.

$taxonomy(string)requis

Nom de la taxonomie.

$before(string)optionnel

Chaîne à utiliser avant les termes.

Valeur par défaut : ''

$sep(string)optionnel

Chaîne à utiliser entre les termes.

Valeur par défaut : ''

$after(string)optionnel

Chaîne à utiliser après les termes.

Valeur par défaut : ''

Retourne

(string|false|WP_Error) Une liste de termes, false s'il n'y a aucuns termes ou WP_Error si échec.

Déclaration et structure de la fonction get_the_term_list()

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;
}
get_the_terms()

Retourne les termes d'une taxonomie attachés à un poste.

esc_url()

Vérifie et nettoie une URL.

get_term_link()

Génère un permalien pour l'archive d'un terme.

apply_filters()

Appel les fonctions qui ont été attaché à un filtre (hook).

is_wp_error()

Vérifie si la variable est une erreur WordPress.

term_links-taxonomy

Filtre les liens de terme pour une taxonomie donnée.

Où est utilisée la fonction get_the_term_list() dans le CMS WordPress

Exemples

Retourner une liste de liens :
echo get_the_term_list( $post->ID, 'people', 'People: ', ', ' );
Retourner une liste HTML :
get_the_term_list( $post->ID, 'styles', '<ul class="styles"><li>', ',</li><li>', '</li></ul>' );

Sources

Codex WordPress : get_the_term_list()

Autres fonctions dans le même fichier : wp-includes/category-template.php

Retour