WordPress的 - 分类下拉列表不与分级工作(Wordpress - taxonomy dro

2019-10-31 07:30发布

任何希望,使这个分类下拉列表具有层次合作。

我说'hierarchical' => 1 ,但似乎对我不起作用!

<?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;
?>

Answer 1:

使用wp_dropdown_categories功能

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'] ); ?>


Answer 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',

); ?>



文章来源: Wordpress - taxonomy dropdown is not working with hierarchical