问题
Any hope to make this Taxonomy dropdown to work with hierarchical.
I added 'hierarchical' => 1
but it seems doesn't work for me!
<?php
if( $terms = get_terms([ 'taxonomy' => 'category', 'hierarchical' => 1, 'hide_empty' => false, 'child_of' => 233 ]) ) :
echo '<select name="categoryfilter4"><option>Downloads...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
回答1:
use wp_dropdown_categories function
https://codex.wordpress.org/Function_Reference/wp_dropdown_categories
<?php wp_dropdown_categories( ['name'=>'categoryfilter4', 'show_option_none' => 'Downloads...','hierarchical' => 1, 'hide_empty' => false, 'child_of' => 233, 'order_by' => 'parent'] ); ?>
回答2:
<?php wp_dropdown_categories( $args ); ?>
<?php $args = array(
'show_option_all' => '',
'show_option_none' => '',
'option_none_value' => '-1',
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 1,
'child_of' => 0,
'exclude' => '',
'include' => '',
'echo' => 1,
'selected' => 0,
'hierarchical' => 0,
'name' => 'cat',
'id' => '',
'class' => 'postform',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => 'category',
'hide_if_empty' => false,
'value_field' => 'term_id',
); ?>
来源:https://stackoverflow.com/questions/52823925/wordpress-taxonomy-dropdown-is-not-working-with-hierarchical