WP_DROPDOWN_CATEGORES()搜索不适用于CPT,请检查我的代码

时间:2018-10-05 作者:dev

搜索无法使用具有自定义分类的CPT

<form id="searchform" method="get" action="<?php bloginfo(\'url\'); ?>">
        <input type="text" name="s" id="s" size="15" />
        <?php //wp_dropdown_categories(\'show_option_none=Select category&taxonomy=type\'); ?>
        <?php wp_dropdown_categories(\'depth=0&orderby=name&hide_empty=1&show_option_all=Search Everything&name=course&taxonomy=type\'); ?>
        <input type="submit" value="Search" />
    </form>
但对于默认的post和category,则使用此代码

<form id="searchform" method="get" action="<?php bloginfo(\'url\'); ?>">
    <input type="text" name="s" id="s" size="15" />
    <?php //wp_dropdown_categories(\'show_option_none=Select category&taxonomy=type\'); ?>
    <?php wp_dropdown_categories(\'depth=0&orderby=name&hide_empty=1&show_option_all=Search Everything\'); ?>
    <input type="submit" value="Search" />

1 个回复
SO网友:dev

搜索CPT的工作下拉列表

<?php

  $categories = get_categories(\'taxonomy=custom-tax\');

  $select = "<select name=\'cat\' id=\'cat\' class=\'postform\'>n";
  $select.= "<option value=\'-1\'>Select category</option>n";

  foreach($categories as $category){
    if($category->count > 0){
        $select.= "<option value=\'".$category->slug."\'>".$category->name."</option>";
    }
  }

  $select.= "</select>";

  echo $select;
?>

<script type="text/javascript"><!--
    var dropdown = document.getElementById("cat");
    function onCatChange() {
        if ( dropdown.options[dropdown.selectedIndex].value != -1 ) {
            location.href = "<?php echo home_url();?>/category/"+dropdown.options[dropdown.selectedIndex].value+"/";
        }
    }
    dropdown.onchange = onCatChange;
--></script> 

结束

相关推荐