Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hchouhan/9558427 to your computer and use it in GitHub Desktop.
Save hchouhan/9558427 to your computer and use it in GitHub Desktop.
Walker Class to extend wp_dropdown_categories and allow it to use term's slug as value instead of ID.
<?php
/*
* A walker class to use that extends wp_dropdown_categories and allows it to use the term's slug as a value rather than ID.
*
* See http://core.trac.wordpress.org/ticket/13258
*
* Usage, as normal:
* wp_dropdown_categories($args);
*
* But specify the custom walker class, and (optionally) a 'id' or 'slug' for the 'value' parameter:
* $args=array('walker'=> new SH_Walker_TaxonomyDropdown(), 'value'=>'slug', .... );
* wp_dropdown_categories($args);
*
* If the 'value' parameter is not set it will use term ID for categories, and the term's slug for other taxonomies in the value attribute of the term's <option>.
*/
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ){
$pad = str_repeat('&nbsp;', $depth * 3);
$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\" 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";
}
}
@hchouhan
Copy link
Author

Displaying the Dropdown

 new SH_Walker_TaxonomyDropdown(),
        'value'=>'slug',
        'show_option_none' =>  __('Select a Category', 'dot'),
        'taxonomy'        =>  'product_cat',
        'id'              =>  'product_cat_dropdown',
        'name'            =>  'product_cat',
        'orderby'         =>  'name',
        'child_of'        => 0,
        //'selected'        =>  $wp_query->query['term'],
        'hierarchical'    =>  true,
        'depth'           =>  0,
        'show_count'      =>  false, // Show # categories in parents
        'hide_empty'      =>  true, // Don't show categories w/o listings
        'exclude'         =>  '19,24'
        )
    );
?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment