Description

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.

Paramètres

$term_id

(int) (Requis) Id du terme parent.

$taxonomy

(string) (Requis) Nom de la taxonomie.

Retourne

(array | WP_Error) Liste d'ids de terme. L'objet WP_Error si $taxonomy n'existe pas.

Structure de la fonction get_term_children()

Définie dans le fichier wp-includes/taxonomy.php à la ligne 1043 :

function get_term_children( $term_id, $taxonomy ) {
    if ( ! taxonomy_exists( $taxonomy ) ) {
        return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
    }

    $term_id = intval( $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ù trouver la fonction get_term_children() dans le CMS Wordpress

Exemple

Afficher une liste désordonnée de liens vers chaque terme enfant :
$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

Retour