get_term_link( WP_Term|int|string $term, string $taxonomy = '' )
Génère un permalien pour l'archive d'un terme.
Description
Il est conseillé de toujours utiliser la fonction is_wp_error()
pour vérifier le résultat de la fonction au cas où le terme n'existerait pas.
Paramètres
$term
(WP_Term | int | string) (Requis) L'objet, l'id ou le slug du terme dont on veut le lien des archives.
$taxonomy
(string) (Optionnel) Taxonomie dont $term fait parti.
Valeur par défaut : ''
Retourne
(string | WP_Error) Url de l'archive du terme. Sinon l'objet WP_Error si le terme n'existe pas.
Structure de la fonction get_term_link()
Définie dans le fichier wp-includes/taxonomy.php à la ligne 4303 :
function get_term_link( $term, $taxonomy = '' ) {
global $wp_rewrite;
if ( ! is_object( $term ) ) {
if ( is_int( $term ) ) {
$term = get_term( $term, $taxonomy );
} else {
$term = get_term_by( 'slug', $term, $taxonomy );
}
}
if ( ! is_object( $term ) ) {
$term = new WP_Error( 'invalid_term', __( 'Empty Term.' ) );
}
if ( is_wp_error( $term ) ) {
return $term;
}
$taxonomy = $term->taxonomy;
$termlink = $wp_rewrite->get_extra_permastruct( $taxonomy );
/**
* Filters the permalink structure for a terms before token replacement occurs.
*
* @since 4.9.0
*
* @param string $termlink The permalink structure for the term's taxonomy.
* @param WP_Term $term The term object.
*/
$termlink = apply_filters( 'pre_term_link', $termlink, $term );
$slug = $term->slug;
$t = get_taxonomy( $taxonomy );
if ( empty( $termlink ) ) {
if ( 'category' === $taxonomy ) {
$termlink = '?cat=' . $term->term_id;
} elseif ( $t->query_var ) {
$termlink = "?$t->query_var=$slug";
} else {
$termlink = "?taxonomy=$taxonomy&term=$slug";
}
$termlink = home_url( $termlink );
} else {
if ( $t->rewrite['hierarchical'] ) {
$hierarchical_slugs = array();
$ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );
foreach ( (array) $ancestors as $ancestor ) {
$ancestor_term = get_term( $ancestor, $taxonomy );
$hierarchical_slugs[] = $ancestor_term->slug;
}
$hierarchical_slugs = array_reverse( $hierarchical_slugs );
$hierarchical_slugs[] = $slug;
$termlink = str_replace( "%$taxonomy%", implode( '/', $hierarchical_slugs ), $termlink );
} else {
$termlink = str_replace( "%$taxonomy%", $slug, $termlink );
}
$termlink = home_url( user_trailingslashit( $termlink, 'category' ) );
}
// Back compat filters.
if ( 'post_tag' === $taxonomy ) {
/**
* Filters the tag link.
*
* @since 2.3.0
* @since 2.5.0 Deprecated in favor of {@see 'term_link'} filter.
* @since 5.4.1 Restored (un-deprecated).
*
* @param string $termlink Tag link URL.
* @param int $term_id Term ID.
*/
$termlink = apply_filters( 'tag_link', $termlink, $term->term_id );
} elseif ( 'category' === $taxonomy ) {
/**
* Filters the category link.
*
* @since 1.5.0
* @since 2.5.0 Deprecated in favor of {@see 'term_link'} filter.
* @since 5.4.1 Restored (un-deprecated).
*
* @param string $termlink Category link URL.
* @param int $term_id Term ID.
*/
$termlink = apply_filters( 'category_link', $termlink, $term->term_id );
}
/**
* Filters the term link.
*
* @since 2.5.0
*
* @param string $termlink Term link URL.
* @param WP_Term $term Term object.
* @param string $taxonomy Taxonomy slug.
*/
return apply_filters( 'term_link', $termlink, $term, $taxonomy );
}
Fonctions et Hooks utilisés par get_term_link()
pre_term_link
Filtre la structure des permaliens pour un terme avant qu'il n'y ait un remplacement.
__()
Retourne la traduction d'un texte.
get_ancestors()
Retourne un tableau d'ids de parents pour un objet donné.
tag_link
Filtre le lien d'une étiquette.
category_link
Filtre le lien d'une catégorie.
term_link
Filtre le lien d'un terme.
get_term_by()
Obtient toute les données d'un terme en fonction d'un champ de recherche donné.
get_term()
Retourne toutes les données d'un terme en donnant son ID.
get_taxonomy()
Retourne l'objet d'une taxonomie en donnant son nom.
home_url()
Retourne l'url du site actuel où le front end est aacessible.
user_trailingslashit()
Retourne un slashe à la fin d'une url si le site est réglé pour les accepter.
apply_filters()
Appel les fonctions qui ont été attaché à un filtre (hook).
is_wp_error()
Vérifie si la variable est une erreur WordPress.
Où trouver la fonction get_term_link()
dans le CMS Wordpress
Exemple
Sources
Codex Wordpress : get_term_link()
Autres fonctions dans le même fichier : wp-includes/taxonomy.php