get_term_children( int $term_id, string $taxonomy )
Rassemble les ids de tous les termes enfants dans un tableau.
Paramètres
$term_id
(int)requisId du terme parent.
$taxonomy
(string)requisNom de la taxonomie.
Description / Informations supplémentaires
Cette fonction récursive rassemblera tous les enfants de $term_id dans un tableau d'ids. Seulement utile pour les taxonomies qui son hiérarchiques.
Retournera un tableau vide si aucun terme n'existe dans la taxonomie.
Retourne
(array|WP_Error) Liste d'ids de terme. L'objet WP_Error si $taxonomy n'existe pas.
Déclaration et structure de la fonction get_term_children()
get_term_children()
est déclarée dans le fichier wp-includes/taxonomy.php
à la ligne 1176 :
function get_term_children( $term_id, $taxonomy ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
}
$term_id = (int) $term_id;
$terms = _get_term_hierarchy( $taxonomy );
if ( ! isset( $terms[ $term_id ] ) ) {
return array();
}
$children = $terms[ $term_id ];
foreach ( (array) $terms[ $term_id ] as $child ) {
if ( $term_id === $child ) {
continue;
}
if ( isset( $terms[ $child ] ) ) {
$children = array_merge( $children, get_term_children( $child, $taxonomy ) );
}
}
return $children;
}
Fonctions utilisées par get_term_children()
__()
Retourne la traduction d'un texte.
_get_term_hierarchy()
Retourne l'id des termes enfants d'une taxonomie.
get_term_children()
Rassemble les ids de tous les termes enfants dans un tableau.
taxonomy_exists()
Détermine si un nom de taxonomie existe.
Où est utilisée la fonction get_term_children()
dans le CMS WordPress
Exemple
$term_id = 10;
$taxonomy_name = 'products';
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
Sources
Codex WordPress : get_term_children()
Autres fonctions dans le même fichier : wp-includes/taxonomy.php