web-dev-qa-db-fra.com

Comment obtenir des parents de taxonomie personnalisée, comme le fait get_category_parents ()?

J'ai une taxonomie personnalisée "catégorie de portefeuille", alors que je dois afficher toutes les catégories de hiérarchie d'une publication de portefeuille, par exemple

le message est sous topcat -> childcat -> yetchildcat, je veux afficher 'topcat, childcat, yetchildcat' sur la page du message, mais get_the_terms only renvoie 'yetchildcat', comment puis-je obtenir tous?

2
Edward

Il existe une fonction plus générique: elle s'appelle get_ancestors() et se trouve dans wp-includes/taxonomy.php

2
scribu

@ Jeff,

Merci pour cela. J'ai corrigé votre fonction cassée $ link. et changé les valeurs par défaut pour inclure "" "en tant que séparateur.

// my own function to wo what get_category_parents does for other taxonomies
function get_taxonomy_parents($id, $taxonomy, $link = true, $separator = ' » ', $nicename = false, $visited = array()) {

$chain = '';

$parent = &get_term($id, $taxonomy);

if (is_wp_error($parent)) { echo "fail";
    return $parent;
}

if ($nicename)

    $name = $parent -> slug;

else

    $name = $parent -> name;

if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {

    $visited[] = $parent -> parent;

    $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);

}

    if ( $link ) {
        // $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
        $chain .= '<a href="' . esc_url( get_term_link( (int) $parent->term_id, $taxonomy ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
    } else {
        $chain .= $name.$separator;
    }
    return $chain;

}
1
Doug Brunner

Il n'y a pas de fonction de niveau plus profond dans get_category_parents() (voir source ). Il vérifie simplement le champ parent dans l'objet category, puis recherche récursivement le parent jusqu'à épuisement.

Vous devrez donc écrire en analogique simple ou le réécrire (selon le nombre de fonctionnalités dont vous avez besoin).

0
Rarst

get_the_terms et get_term récupèrent l'ID parent parmi les données du terme. Vous pouvez donc utiliser ces informations pour créer des fonctions personnalisées qui afficheront l’arbre de taxonomie.

Je travaille actuellement sur quelque chose de similaire - posterai le code dès que je le ferai fonctionner et le nettoyerai ;-D

EDIT: Je l’ai enfin fait ... Voici le code pour obtenir "topcat -> childcat -> yetchildcat" avec un lien vers la page du terme de taxonomie:

$terms = get_the_terms( $post->id, 'portfolio category' );
if ( $terms && ! is_wp_error( $terms ) ) { 
    foreach ( $terms as $term ) {
        $tree = '<a href="'.get_term_link($term->slug, 'portfolio category').'">'.$term->name.'</a>';
        $parents = get_ancestors( $term->term_id, 'portfolio category' );
        foreach ($parents as $parent) {
            $term = get_term($parent, 'portfolio category');
            $tree = '<a href="'.get_term_link($term->slug, 'geo').'">'.$term->name.'</a> -> '.$tree;
       }
    echo $tree;
}

Je suis sûr qu'il est possible de créer un code meilleur/plus propre, mais cela semble fonctionner ;-D

0
LapinLove404

J'ai écrit une fonction récursive basée presque exactement sur get_category_parents (), remplaçant simplement les parties spécifiques à la catégorie par des équivalents basés sur les termes. J'espère que ça aide quelqu'un.

// my own function to wo what get_category_parents does for other taxonomies
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {

$chain = '';

$parent = &get_term($id, $taxonomy);

if (is_wp_error($parent)) { echo "fail";
    return $parent;
}

if ($nicename)

    $name = $parent -> slug;

else

    $name = $parent -> name;

if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {

    $visited[] = $parent -> parent;

    $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);

}

if ( $link ) {

    $chain .= '<a href="' . esc_url( get_term_link( (int) $parent->term_id, $taxonomy ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
} else {
    $chain .= $name.$separator;
}
return $chain;

}
0
Jeff