get_terms() non restituisce i termini vuoti anche se hide_empty è false

1 set 2014, 14:32:31
Visualizzazioni: 23.8K
Voti: 4

Non riesco a far restituire a get_terms() i termini vuoti e ho provato in diversi modi.

Ecco il codice:

$terms = get_terms('device',array('hide_empty' => 0));
foreach($terms as $term) {
        if($term->parent == 0) {
            if($i++ != 0) echo '</optgroup>'; echo '<optgroup label="'.$term->name.'">';
            $id = $term->term_id;
            $args = array("child_of"=>$id);
            $this_term = get_terms('device',$args);
            foreach($this_term as $the_term) { 
                $term_name = str_replace($term->name,'',$the_term->name);
                echo '<option value="'.$the_term->term_id.'">'.$the_term->name.'</option>';
            }
        }
    }

Ho provato tutti i modi possibili:

$terms = get_terms('device',array('hide_empty' => false))
$terms = get_terms('device',array('hide_empty' => 0))
$terms = get_terms('device',array('hide_empty=false'))
$terms = get_terms('device',array('hide_empty=0'))

Ho provato anche gli ultimi due modi senza array. Niente sembra funzionare. Restituisce tutti i termini che hanno post, ma nessuno vuoto.

3
Commenti

Hai provato con il parametro 'get' => 'all'?

Tomás Cot Tomás Cot
1 set 2014 15:05:37

Ho trovato il colpevole. Stavo usando get_terms() due volte e me ne ero dimenticato, quindi non ho aggiunto hide_empty alla seconda funzione.

Alex Dumitru Alex Dumitru
1 set 2014 15:06:51

Ok, sono contento di sentirlo.

Tomás Cot Tomás Cot
1 set 2014 15:08:16
Tutte le risposte alla domanda 2
1
10

Stai utilizzando l'argomento hide_empty per $terms, ma non per $this_term all'interno del tuo ciclo.

Inoltre, con il modo in cui stai generando il tuo select, sarebbe molto più efficiente interrogare solo i termini di livello superiore per il ciclo principale:

$terms = get_terms( 'device', array( 'hide_empty' => false, 'parent' => 0 ) );

E poi rimuovere if($term->parent == 0) {... all'interno del tuo ciclo.

1 set 2014 15:07:36
Commenti

Sì, così sembra molto meglio. Ho già apportato la modifica. Grazie!

Alex Dumitru Alex Dumitru
1 set 2014 15:09:22
0

Ho trovato il problema.

Sto usando get_terms() due volte, una per i termini genitori e una per i figli. Sembra che non abbia aggiunto hide_empty=false a quello che recupera i termini figli.

1 set 2014 15:05:02