Skip to content

Instantly share code, notes, and snippets.

@maor
Last active December 12, 2015 09:19
Show Gist options
  • Save maor/4750468 to your computer and use it in GitHub Desktop.
Save maor/4750468 to your computer and use it in GitHub Desktop.
<?php
function lt_admin_show_taxonomies_dropdowns() {
if ( 'lt_product' != get_current_screen()->post_type )
return;
$taxonomies = get_taxonomies( array(
'object_type' => array( 'lt_product' )
), 'objects' );
foreach ( $taxonomies as $t => $details )
wp_dropdown_categories( array(
'show_option_all' => sprintf( __( 'View %s' ), $details->labels->all_items ),
'hide_empty' => 0,
'hierarchical' => 1,
'show_count' => 0,
'orderby' => 'name',
'selected' => isset( $GLOBALS[ $t ] ) ? $GLOBALS[ $t ] : '0',
'taxonomy' => $t,
'name' => $t,
'walker' => new Lateltin_Walker_Taxonomies_Dropdown,
) );
}
add_action( 'restrict_manage_posts', 'lt_admin_show_taxonomies_dropdowns' );
/**
* Custom walker that echos slugs instead of IDs
*
* @since 1.0
*/
class Lateltin_Walker_Taxonomies_Dropdown extends Walker {
var $tree_type = 'category';
var $db_fields = array(
'parent' => 'parent',
'id' => 'term_id'
);
/**
* @see Walker::start_el()
* @since 2.1.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $category Category data object.
* @param int $depth Depth of category. Used for padding.
* @param array $args Uses 'selected' and 'show_count' keys, if they exist.
*/
function start_el( &$output, $category, $depth, $args, $id = 0 ) {
$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->slug == $args['selected'] )
$output .= ' selected="selected"';
$output .= '>';
$output .= $pad.$cat_name;
if ( $args['show_count'] )
$output .= '&nbsp;&nbsp;('. $category->count .')';
$output .= "</option>\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment