Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I asked this on the Wordpress forum but dint get any replies now I hope I can ask it here.

I found the code here My Code is:

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>

It creates a drop down for my custom taxonomy. It works 100% I just cant get it to show my categories with hierarchy like it would with wp_dropdown_categories.

I have tried to use wp_dropdown_categories with a walker but it outputs a slug website.com?equipment_cat instead of website.com/equipment_cat.

Here is my code for the wp_dropdown_categories with walker.

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

Help would really be appreciated.

share|improve this question
    
When using the wp_dropdown_categories code my url is /?equipment_cat=ripper&kw_min=&kw_max=&pr_min=&pr_max= (doens't work) but when I use the terms code the url is /equipment-cat/implemente/ripper/?kw_min&kw_max&pr_min&pr_max (works) how can I get the url to work with wp_dropdown_categories? –  HaneD Nov 22 '12 at 13:06
add comment

1 Answer

up vote 0 down vote accepted

I can kick myself, been struggling for 2 days and all that I had to do to make the wp_dropdown_categories option work is change the name to the same value as the taxonomy.

My complete working code is:

<?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>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.