Ottenere l'ID del termine corrente
Sto usando il seguente codice per ottenere un array di tassonomie figlie e scriverle con i link in una lista non ordinata.
<?php
// ID del termine genitore
$termID = 10;
// Nome della tassonomia
$taxonomyName = "products";
$termchildren = get_term_children( $termID, $taxonomyName );
echo '<ul>';
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
?>
Quello che sto cercando di ottenere è l'ID del termine (categoria) attuale in modo da poterlo sostituire su $termID e non dover codificare manualmente l'ID del termine.
Qualsiasi aiuto sarebbe molto apprezzato!
Grazie!

Ecco una funzione che utilizzo per elencare i sotto-termini:
/**
* Elenca tutte le sotto-voci di una tassonomia.
*
* @return void
*/
function ttt_get_subterms( $args = array () )
{
if ( ! isset ( get_queried_object()->taxonomy ) )
{
return;
}
$options = array (
'child_of' => get_queried_object_id()
, 'echo' => 0
, 'taxonomy' => get_queried_object()->taxonomy
, 'title_li' => FALSE
, 'use_desc_for_title' => FALSE
);
$settings = array_merge( $options, $args );
$subtermlist = wp_list_categories( $settings );
// Senza risultati WP crea un elemento fittizio. Non contiene link.
! empty ( $subtermlist ) and FALSE !== strpos( $subtermlist, '<a ' )
and print "<ul class=subterms>$subtermlist</ul>";
}
Usala come wp_list_categories()
.
Evita get_term_by()
. È molto costoso e non necessario.

Per ottenere il termine corrente puoi usare get_query_var( 'term' );
e per ottenere la tassonomia corrente puoi usare get_query_var( 'taxonomy' )
, quindi puoi fare qualcosa come questo:
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
echo '<ul>';
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';

Oppure puoi usare: term_exists( $term, $taxonomy, $parent )
$term_id = term_exists( $term_name );
Vedi WordPress Codex
Verifica se un determinato termine esiste e restituisce l'ID del termine
Restituisce l'ID del termine se non è stata specificata una tassonomia e il termine esiste.

Per ottenere l'ID del termine corrente, utilizza:
$term_id = get_queried_object()->term_id;
get_query_var
non può essere utilizzato in questo caso, poiché term_id
non è incluso nella lista delle variabili disponibili pubblicamente.
