如何在wp_dropdown_categories中更改“选项"和“选择"的值? [英] How to change value for 'options' and 'select' in wp_dropdown_categories?

查看:96
本文介绍了如何在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?

推荐答案

好,但是我回答了另一个我自己的问题.我能够创建一个下拉过滤器来检索当前正在查看的分类法页面的子术语.对于对解决方案感兴趣的人,这里是---

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

首先,我必须在我的functions.php文件中创建一个沃克类-

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

然后我将这段代码放在侧边栏-

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

这篇关于如何在wp_dropdown_categories中更改“选项"和“选择"的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆