1

我目前正在使用此代码仅显示正在查看的当前类别的子术语 -

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

我需要能够向 's 和开头添加值和类。我怎样才能修改上面的代码来做到这一点?

我不确定,但我想我已经接近了。-

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

1 回答 1

4

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

首先我必须在我的functions.php文件中创建一个walker类——

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 );
?>
于 2013-01-31T07:08:11.683 回答