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.

Alex Dumitru
753
Commenti
Tutte le risposte alla domanda
2
1
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.

TheDeadMedic
36.6K
1 set 2014 15:07:36
Domande correlate
2
risposte
1
risposte