2012-12-29 18 views
1

我目前使用此代碼只顯示當前類別的子項正在查看 -如何更改wp_dropdown_categories中'選項'和'選擇'的值?

<?php 

//first get the current term 
$current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); 

//then set the args for wp_dropdown_categories 
$args = array(
    'child_of' => $current_term->term_id, 
    'taxonomy' => $current_term->taxonomy, 
    'hide_empty' => 0, 
    'hierarchical' => true, 
    'depth' => 2, 
    'title_li' => '', 
     'show_option_all' => All, 
     'hide_if_empty' => true 
    ); 
wp_dropdown_categories($args); 
?> 

我需要能值和類添加到的和開放。我如何修改上面的代碼來做到這一點?

林不知道,但我認爲我越來越接近。 -

<?php 
function get_terms_dropdown($taxonomies, $args){ 
//first get the current term 
$current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); 
$args = array(
    'child_of' => $current_term->term_id, 
    'taxonomy' => $current_term->taxonomy, 
    'hide_empty' => 0, 
    'hierarchical' => true, 
    'depth' => 2, 
    'title_li' => '', 
     'show_option_all' => All, 
     'hide_if_empty' => true 
    ); 
    $output ="<select name='".$term_slug."'><option selected='".$selected."' value='".$emptyvalue."'>Select a Category</option>'"; 

    foreach($current_term as $term){ 
     $output .="<option name='".$term_slug."' value='".$link."'>".$term_name."</option>"; 
    } 
    $output .="</select>"; 
return $output; 
} 
echo get_terms_dropdown($taxonomies, $args); 

?> 

問題是下拉式不顯示任何類別/術語,我錯過了什麼?

回答

4

好吧,我回答了另一個我自己的問題。我能夠創建下拉篩選器,檢索正在查看的當前分類頁面的子項。對於任何有興趣的解決方案,這就是---

首先,我在我的functions.php文件創建一個學步車類 -

class SH_Walker_TaxonomyDropdown extends Walker_CategoryDropdown{ 

    function start_el(&$output, $category, $depth, $args) { 
     $pad = str_repeat('&nbsp;', $depth * 2); 
     $cat_name = apply_filters('list_cats', $category->name, $category); 

     if(!isset($args['value'])){ 
      $args['value'] = ($category->taxonomy != 'category' ? 'slug' : 'id'); 
     } 

     $value = ($args['value']=='slug' ? $category->slug : $category->term_id); 

     $output .= "\t<option class=\"level-$depth\" data-filter-value=\".".$value."\""; 
     if ($value === (string) $args['selected']){ 
      $output .= ' selected="selected"'; 
     } 
     $output .= '>'; 
     $output .= $pad.$cat_name; 
     if ($args['show_count']) 
      $output .= '&nbsp;&nbsp;('. $category->count .')'; 

     $output .= "</option>\n"; 
} 
} 

然後我把這個代碼在我的側邊欄 -

<?php 

//first get the current term 
$current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); 

//then set the args for wp_dropdown_categories 
$args = array(
    'walker'=> new SH_Walker_TaxonomyDropdown(), 
    'value'=>'slug', 
    'child_of' => $current_term->term_id, 
    'taxonomy' => $current_term->taxonomy, 
    'hide_empty' => 0, 
    'hierarchical' => true, 
    'depth' => 2, 
    'title_li' => '', 
    'id' => 'filter-select', 
    'class' => 'filter option-set', 
     'show_option_all' => All, 
     'hide_if_empty' => true 
    ); 
wp_dropdown_categories($args); 
?> 
相關問題