2017-06-09 112 views
1

我有下面的代码,显示在我的Wordpress站点下拉菜单与职位类别,然后显示一篇文章。我想要做的是为“所有类别”添加一个选项。我不想使用'show_option_all' => 'All Categories',,因为这会将选项放在顶部,而我希望它位于底部,而且在某些页面上也会自动选中,但我不会进入该选项。只需说我想在我的列表末尾手动插入一个选项。将选项添加到wp_dropdown_categories

这里是我的代码

<form id="category-select" class="category-select" action="<?php echo esc_url(home_url('/')); ?>" method="get"> 
    <?php 
     $args = array(
      'show_option_none' => __('Select Category'), 
      'show_option_all' => 'All Categorys', 
      'show_count'  => 1, 
      'orderby'   => 'name', 
      'echo'    => 0, 
    ); 
     $select = wp_dropdown_categories($args); 
     $replace = "<select$1 onchange='return this.form.submit()'>"; 
     $select = preg_replace('#<select([^>]*)>#', $replace, $select); 
     echo $select; 
    ?> 
    <noscript> 
     <input type="submit" value="View" /> 
    </noscript> 
</form> 

感谢。

任何帮助表示赞赏。

伊恩

回答

1

最佳选择是使用get_terms()函数,并创建类别下拉菜单按您的要求。

$terms = get_terms([ 
 
    'taxonomy' => $taxonomy, 
 
    'hide_empty' => false, 
 
]); 
 
<select> 
 
foreach($terms as $cat) 
 
{ 
 
echo '<option value="'.$cat->term_id.'">$cat->name</option>'; 
 
} 
 
<option value="" selected="selected">All Categories</option> 
 
</select>