View difference between Paste ID: scckuBhp and
SHOW: | | - or go back to the newest paste.
1-
1+
<?php
2
3
/* Forked version of wp_dropdown_categories() */
4
function mfields_dropdown_taxonomy_terms( $args = '' ) {
5
	$defaults = array(
6
		'show_option_all' => '', 'show_option_none' => '',
7
		'orderby' => 'id', 'order' => 'ASC',
8
		'show_last_update' => 0, 'show_count' => 0,
9
		'hide_empty' => 1, 'child_of' => 0,
10
		'exclude' => '', 'echo' => 1,
11
		'selected' => 0, 'hierarchical' => 0,
12
		'name' => 'cat', 'class' => 'postform',
13
		'depth' => 0, 'tab_index' => 0
14
	);
15
	
16
	$defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
17
18
	$r = wp_parse_args( $args, $defaults );
19
20
	if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
21
		$r['pad_counts'] = true;
22
	}
23
24
	$r['include_last_update_time'] = $r['show_last_update'];
25
	extract( $r );
26
27
	$tab_index_attribute = '';
28
	if ( (int) $tab_index > 0 )
29
		$tab_index_attribute = " tabindex=\"$tab_index\"";
30
31
	$categories = get_categories( $r );
32
	$name = esc_attr($name);
33
	$class = esc_attr($class);
34
	
35
	$id = ( !empty( $id ) ) ? esc_attr( $id ) : $name;
36
	
37
	$output = '';
38
	if ( ! empty( $categories ) ) {
39
		$output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
40
41
		if ( $show_option_all ) {
42
			$show_option_all = apply_filters( 'list_cats', $show_option_all );
43
			$selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
44
			$output .= "\t<option value='0'$selected>$show_option_all</option>\n";
45
		}
46
47
		if ( $show_option_none ) {
48
			$show_option_none = apply_filters( 'list_cats', $show_option_none );
49
			$selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" : '';
50
			$output .= "\t<option value='-1'$selected>$show_option_none</option>\n";
51
		}
52
53
		if ( $hierarchical )
54
			$depth = $r['depth'];  // Walk the full depth.
55
		else
56
			$depth = -1; // Flat.
57
		
58
		$output .= mfields_walk_taxonomy_dropdown_tree( $categories, $depth, $r );
59
		$output .= "</select>\n";
60
	}
61
62
	$output = apply_filters( 'wp_dropdown_cats', $output );
63
64
	if ( $echo )
65
		echo $output;
66
67
	return $output;
68
}
69
/* Forked version of walk_category_dropdown_tree() */
70
function mfields_walk_taxonomy_dropdown_tree() {
71
	$args = func_get_args();
72
	// the user's options are the third parameter
73
	if ( empty( $args[2]['walker'] ) || !is_a( $args[2]['walker'], 'Walker' ) )
74
		$walker = new mfields_walker_taxonomy_dropdown;
75
	else
76
		$walker = $args[2]['walker'];
77
78
	return call_user_func_array( array( &$walker, 'walk' ), $args );
79
}
80
/* Forked version of Walker_CategoryDropdown */
81
class mfields_walker_taxonomy_dropdown extends Walker {
82
	var $tree_type = 'category';
83
	var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
84
	
85
	function start_el( &$output, $category, $depth, $args ) {
86
		$pad = str_repeat( '&nbsp;', $depth * 3 );
87
		$cat_name = apply_filters( 'list_cats', $category->name, $category );
88
		
89
		if( $category->taxonomy === 'category' )
90
			$value = esc_attr( $category->term_id );
91
		else
92
			$value = esc_attr( $category->slug );
93
			
94
		$output .= "\t<option class=\"level-$depth\" value=\"" . $value . "\"";
95
		
96
		if ( $category->taxonomy === 'category' ) {
97
			if ( $category->term_id == $args['selected'] )
98
				$output .= ' selected="selected"';
99
		}
100
		else {
101
			if ( $category->slug == $args['selected'] )
102
				$output .= ' selected="selected"';
103
		}
104
			
105
		$output .= '>';
106
		$output .= $pad . $cat_name;
107
		if ( $args['show_count'] )
108
		$output .= '&nbsp;&nbsp;('. $category->count .')';
109
		if ( $args['show_last_update'] ) {
110
			$format = 'Y-m-d';
111
			$output .= '&nbsp;&nbsp;' . gmdate( $format, $category->last_update_timestamp );
112
		}
113
		$output .= "</option>\n";
114
	}
115
}
116
117
?>