使用$TERMS的分层视图的分类下拉列表

时间:2012-11-22 作者:HaneD

我在Wordpress论坛上问过这个问题,但现在没有得到任何回复,我希望我可以在这里问。

我找到密码了here 我的代码是:

function exc_custom_taxonomy_dropdown( $taxonomy, $orderby, $order, $hierarchical, $show_count,  $name) {
    $args = array(
        \'orderby\' => \'name\',
        \'order\' => \'ASC\',
        \'hierarchical\' => true,
        \'show_count\' => true,
    );
    $terms = get_terms( $taxonomy, $args );
    $name = ( $name ) ? $name : $taxonomy;
    if ( $terms ) {
        printf( \'<select name="%s" class="postform">\', $name );
        foreach ( $terms as $term ) {
            printf( \'<option value="%s">%s (%s)</option>\', $term->slug, $term->name, $term->count );
        }
        print( \'</select>\' );
    }
}
?>
<form role="search" method="get" id="equipfilter" action="<?php bloginfo(\'url\'); ?>">
        <fieldset>
           <?php exc_custom_taxonomy_dropdown( \'exc_equipment_cat\', \'name\', \'ASC\', true, true, \'exc_equipment_cat\'); ?>
-       </fieldset>
        <fieldset>
            <legend>Kw Range:</legend>
            <input type="text" name="kw_min" placeholder="from" value><br />
            <input type="text" name="kw_max" placeholder="to" value>
        </fieldset>
        <fieldset>
            <legend>Price Range:</legend>
            <input type="text" name="pr_min" placeholder="from" value><br />
            <input type="text" name="pr_max" placeholder="to" value>
        </fieldset>
        <input type="submit" id="filtersubmit" value="Search" />
    </form>
它为我的自定义分类法创建了一个下拉列表。它100%工作,我只是不能让它显示我的类别与层次结构,就像它与wp\\U下拉式菜单\\U类别。

我曾尝试将wp\\u dropdown\\u categories用于walker,但它会输出一个slug网站。com?equipment\\u cat代替网站。com/equipment\\u目录。

这是我的代码,用于walker的wp\\u下拉列表\\u类别。

<?php
// Equipment Category Dropdown, thanks https://gist.github.com/2902509
class Walker_SlugValueCategoryDropdown extends Walker_CategoryDropdown {

        function start_el(&$output, $category, $depth, $args) {
        $pad = str_repeat(\'&nbsp;\', $depth * 3);

        $cat_name = apply_filters(\'list_cats\', $category->name, $category);
        $output .= "\\t<option class=\\"level-$depth\\" value=\\"".$category->slug."\\"";
        if ( $category->term_id == $args[\'selected\'] )
            $output .= \' selected="selected"\';
        $output .= \'>\';
        $output .= $pad.$cat_name;
        if ( $args[\'show_count\'] )
            $output .= \'&nbsp;&nbsp;(\'. $category->count .\')\';
        if (isset ( $args[\'show_last_update\'] ) ) {
            $format = \'Y-m-d\';
            $output .= \'&nbsp;&nbsp;\' . gmdate($format, $category->last_update_timestamp);
        }
        $output .= "</option>\\n";
    }
}
?>
<form role="search" method="get" id="equipfilter" action="<?php bloginfo(\'url\'); ?>">
        <fieldset>
            <?php
            $dropdown_args = array(
                \'taxonomy\'      => \'exc_equipment_cat\',
                \'name\'          => \'equipment_cat\',
                \'show_option_none\'  => \'Select category\',
                \'show_count\'        => 1,
                \'orderby\'       => \'name\',
                \'hierarchical\'      => true,
                \'echo\'          => 1,
                \'walker\'            => new Walker_SlugValueCategoryDropdown);

            wp_dropdown_categories( $dropdown_args );
            ?>
        </fieldset>
        <fieldset>
            <legend>Kw Range:</legend>
            <input type="text" name="kw_min" placeholder="from" value><br />
            <input type="text" name="kw_max" placeholder="to" value>
        </fieldset>
        <fieldset>
            <legend>Price Range:</legend>
            <input type="text" name="pr_min" placeholder="from" value><br />
            <input type="text" name="pr_max" placeholder="to" value>
        </fieldset>
        <input type="submit" id="filtersubmit" value="Search" />
    </form>
非常感谢您的帮助。

1 个回复
最合适的回答,由SO网友:HaneD 整理而成

我可以踢自己,已经挣扎了2天,要让wp\\u dropdown\\u categories选项工作,我所要做的就是将名称更改为与分类法相同的值。

我的完整工作代码是:

<?php
// Equipment Category Dropdown, thanks https://gist.github.com/2902509
class Walker_SlugValueCategoryDropdown extends Walker_CategoryDropdown {

        function start_el(&$output, $category, $depth, $args) {
        $pad = str_repeat(\'&nbsp;\', $depth * 3);

        $cat_name = apply_filters(\'list_cats\', $category->name, $category);
        $output .= "\\t<option class=\\"level-$depth\\" value=\\"".$category->slug."\\"";
        if ( $category->term_id == $args[\'selected\'] )
            $output .= \' selected="selected"\';
        $output .= \'>\';
        $output .= $pad.$cat_name;
        if ( $args[\'show_count\'] )
            $output .= \'&nbsp;&nbsp;(\'. $category->count .\')\';
        if (isset ( $args[\'show_last_update\'] ) ) {
            $format = \'Y-m-d\';
            $output .= \'&nbsp;&nbsp;\' . gmdate($format, $category->last_update_timestamp);
        }
        $output .= "</option>\\n";
    }
}
?>
<form role="search" method="get" id="equipfilter" action="<?php bloginfo(\'url\'); ?>">
        <fieldset>
            <?php
            $dropdown_args = array(
                \'taxonomy\'      => \'exc_equipment_cat\',
                \'name\'          => \'exc_equipment_cat\',
                \'show_option_none\'  => \'Select category\',
                \'show_count\'        => 1,
                \'orderby\'       => \'name\',
                \'hierarchical\'      => true,
                \'echo\'          => 1,
                \'walker\'            => new Walker_SlugValueCategoryDropdown);

            wp_dropdown_categories( $dropdown_args );
            ?>
        </fieldset>
        <fieldset>
            <legend>Kw Range:</legend>
            <input type="text" name="kw_min" placeholder="from" value><br />
            <input type="text" name="kw_max" placeholder="to" value>
        </fieldset>
        <fieldset>
            <legend>Price Range:</legend>
            <input type="text" name="pr_min" placeholder="from" value><br />
            <input type="text" name="pr_max" placeholder="to" value>
        </fieldset>
        <input type="submit" id="filtersubmit" value="Search" />
    </form>

结束

相关推荐

如何使用wp_INSERT_TERMS将术语添加到分类中?

我有一个巨大的excel位置数据库,我想将其作为术语添加到我当前的分类法中。然而,我不知道最好的方法是什么,因为手动插入可能需要数周的时间<我已经看过了:http://codex.wordpress.org/Function_Reference/wp_insert_term但不确定如何正确使用它。我的分类法叫做:location我想添加以下示例术语:London, Glasgow, Bristol, Bournemouth这可能吗?此外,如果存在层次关系,是否也可以自动添加?例如:伦敦(父母)、肯辛