Ottenere l'ID del termine corrente

21 apr 2011, 04:25:29
Visualizzazioni: 32.4K
Voti: 2

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!

0
Tutte le risposte alla domanda 4
1

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.

21 apr 2011 05:13:41
Commenti

Funziona alla grande e ottengo i termini elencati proprio come wp_list_categories. Grazie!

Rise Rise
21 apr 2011 05:38:31
0

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>';
21 apr 2011 04:43:43
0

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.

4 feb 2013 13:19:01
0

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.

21 mag 2018 19:35:05