Come usare get_term_children per ottenere la sottocategoria di una categoria padre per il post corrente

20 apr 2014, 11:13:59
Visualizzazioni: 16.9K
Voti: 1

Nel mio tema WooCommerce ho creato una categoria radice: 'Brands' e per ogni prodotto ho aggiunto il suo brand come sottocategoria della categoria genitore 'Brands'.

Nella pagina del prodotto voglio visualizzare il brand del prodotto corrente.

global $post;

// Ottieni l'ID del termine 'brands' dallo slug
$brands_id = get_term_by('slug', 'brands', 'product_cat');

// Ottieni i figli di quel termine 
$termchildren = get_term_children( $brands_id, 'product_cat' );

// Cicla attraverso i figli del termine 'brand' per visualizzare il nome del brand
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, 'product_cat' );
    echo '<a href="' . get_term_link( $child, 'product_cat' ) . '">' . $term->name . '</a>';
}

Questo codice visualizza sempre tutti i brand. Come posso filtrare queste informazioni per mostrare solo i brand associati al prodotto corrente?

4
Commenti

Perché assumi che dovrebbe filtrare in quel modo? Non ricordo che gli argomenti accettino term_id in quel contesto.

Rarst Rarst
20 apr 2014 15:56:03

Sì, sembra che tu abbia ragione, mi stavo confondendo con l'array di ritorno

tommyf tommyf
20 apr 2014 16:32:39

Risolto allora? :) O per favore aggiorna la tua domanda.

Rarst Rarst
20 apr 2014 17:36:01

Domanda aggiornata.

tommyf tommyf
20 apr 2014 22:30:45
Tutte le risposte alla domanda 3
0

Non c'è bisogno di usare get_term_children() per niente. Basta ciclare attraverso get_the_terms() per ottenere ciò che serve.

global $post;
$brands_id = get_term_by('slug', 'brands', 'product_cat');

$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
    if($term->parent === $brands_id->term_id) {
        echo $term->name;
        break;
    }
}
21 apr 2014 08:28:38
0
$term_id       = get_term_by( 'slug', 'brands', 'product_cat' );
$taxonomy_name = 'product_cat';
$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>';
21 apr 2014 08:43:45
0

Prima di tutto ho posto la stessa domanda qui e ho trovato la risposta da solo :)

https://wordpress.stackexchange.com/questions/188839/wordpress-tags-and-hierarchy-parent-child-relation

Ecco come dovresti farlo:

    $taxonomies = array( 
        'brands'
    );

    $args = array(
        'orderby'           => 'name', 
        'order'             => 'ASC',
        'fields'            => 'all',
        'parent'            => '(parentID)',
        'hierarchical'      => true, 
        'child_of'          => 0
    ); 

    $terms = get_terms($taxonomies, $args);
    var_dump($terms);
    foreach ($terms as $term) {
        print '<h2 class="story-heading">'.$term->name.'</h2>';
    } 

Se conosci già l'ID della categoria genitore, lo passi semplicemente come INT e nel ciclo foreach otterrai tutti i termini figlio (sottocategorie) del genitore. Inoltre, se hai bisogno di riutilizzare questo codice, cioè se vuoi visualizzare più volte la stessa query e ottenere diversi termini genitore -> figlio, ti consiglio di scrivere una funzione e inserirla nel file functions.php. La funzione dovrebbe essere simile a questa:

function get_children_of_parent_terms($tax, $pid) {
  $taxonomies = $tax;

        $args = array(
            'orderby'           => 'name', 
            'order'             => 'ASC',
            'fields'            => 'all',
            'parent'            => $pid,
            'hierarchical'      => true, 
            'child_of'          => 0
        ); 

        $terms = get_terms($taxonomies, $args);
        var_dump($terms);
        foreach ($terms as $term) {
            print '<h2 class="story-heading">'.$term->name.'</h2>';
        }
   }

add_action('init','get_children_of_parent_terms');

E poi ogni volta che vuoi interrogare i figli del termine genitore (categoria), basta chiamare questa funzione con il nome della categoria genitore della tassonomia come $tax e l'id del genitore come $pid, ad esempio echo get_children_of_parent_terms('brands','15'); e supponendo che tu abbia la tassonomia "brands" e qualche termine genitore con id 15, questo mostrerà tutti i termini figlio del termine con ID 15.

Saluti.

23 mag 2015 12:14:48