Come usare get_term_children per ottenere la sottocategoria di una categoria padre per il post corrente
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?

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;
}
}

$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>';

Prima di tutto ho posto la stessa domanda qui e ho trovato la risposta da solo :)
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.
