dongqing314511 2012-12-29 03:58
浏览 51
已采纳

如何在wp_dropdown_categories中更改“选项”和“选择”的值?

I am currently using this code to display only the child terms of the current category being viewed -

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

I need to be able to add values and classes to the 's and the opening . How can I modify the above code to do so?

Im not sure but I think Im getting close. -

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

?>

The problem is the drop down is not showing any categories/terms, what am I missing?

  • 写回答

1条回答 默认 最新

  • duandao8607 2013-01-31 07:08
    关注

    Ok yet I answered another one of my own questions. I was able to create the drop down filter that retrieves the child terms of the current taxonomy page being viewed. For anyone interested in the solution, here it is ---

    First I had to create in my functions.php file a walker class --

    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>
    ";
    }
     }
    

    Then I placed this code in my sidebar --

    <?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 );
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。