Elenca tutti i termini di tassonomia / Mostra link se ci sono post collegati, altrimenti mostra solo nomi

8 nov 2014, 11:34:41
Visualizzazioni: 18.6K
Voti: 3

Sto cercando un modo per elencare tutti i termini di una tassonomia personalizzata. Solo i termini che hanno post collegati dovrebbero avere link alla pagina di archivio. Se non ci sono post collegati dovrebbe mostrare solo i nomi.

Qualche idea? Grazie!

<?php
$taxonomy = 'cat';
$queried_term = get_term_by( 'slug', get_query_var($taxonomy) );
$terms = get_terms($taxonomy);
if ( $terms !== 0 ) {
    foreach ( $terms as $term ) {
        echo $term->name . ", ";
    }
}
if ( $terms > 0 ) {
    foreach ( $terms as $term ) {
        echo '<li><a href="' . $term->slug . '">' . $term->name .'</a></li>';
    }
}
?>
0
Tutte le risposte alla domanda 3
1

Non ho capito bene la tua domanda, ma prova questo. La spiegazione è nei commenti.

// nome della tua tassonomia
$tax = 'post_tag';

// ottieni i termini della tassonomia
$terms = get_terms( $tax, [
  'hide_empty' => false, // non nascondere i termini vuoti
]);

// cicla attraverso tutti i termini
foreach( $terms as $term ) {

  // se non ci sono voci associate al termine
  if( 0 == $term->count )
    // mostra solo il nome del termine
    echo '<h4>' . $term->name . '</h4>';

  // se il termine ha più di 0 voci
  elseif( $term->count > 0 )
    // mostra il link all'archivio del termine
    echo '<h4><a href="'. get_term_link( $term ) .'">'. $term->name .'</a></h4>';

}

Spero ti sia stato utile.

8 nov 2014 11:56:09
Commenti

Fantastico, ha funzionato perfettamente :)

Jodyshop Jodyshop
3 ott 2021 18:05:14
1

Grazie per il tuo aiuto! Ho fatto alcune piccole modifiche e ora funziona:

<?php
// il nome della tua tassonomia
$tax = 'cat';

// ottieni i termini della tassonomia
$terms = get_terms( $tax, $args = array(
  'hide_empty' => false, // non nascondere i termini vuoti
));

// scorri tutti i termini
foreach( $terms as $term ) {

    // Ottieni il link del termine
    $term_link = get_term_link( $term );

    if( $term->count > 0 )
        // mostra il link all'archivio del termine
        echo '<a href="' . esc_url( $term_link ) . '">' . $term->name .'</a>';

    elseif( $term->count !== 0 )
        // mostra il nome
        echo '' . $term->name .'';
}
?>

Fantastico!

8 nov 2014 12:14:45
Commenti

Funziona alla grande! C'è un modo per ottenere la tassonomia corrente in cui mi trovo ed evidenziarla? Grazie

gil hamer gil hamer
19 mag 2016 10:23:41
0

Solo un miglioramento al commento di Schakelen per testare se viene restituito qualcosa

// il nome della tua tassonomia
$tax = 'cat';

// ottieni i termini della tassonomia
$terms = get_terms( $tax, $args = array( 
'hide_empty' => false, // non nascondere i termini vuoti
));

    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){                

        // ciclo attraverso tutti i termini
        foreach( $terms as $term ) {

            // Ottieni il link del termine
            $term_link = get_term_link( $term );

            if( $term->count > 0 )
                // mostra il link all'archivio del termine
                echo '<a href="' . esc_url( $term_link ) . '">' . $term->name .'</a>';

            elseif( $term->count !== 0 )
                // mostra il nome
                echo '' . $term->name .'';
        }
    }
1 giu 2017 20:37:11