Advertisements

wp_dropdown_categories

Definition:
function wp_dropdown_categories( $args = '' ) {}

Display or retrieve the HTML dropdown list of categories.
The list of arguments is below: ‘show_option_all’ (string) – Text to display for showing all categories. ‘show_option_none’ (string) – Text to display for showing no categories. ‘orderby’ (string) default is ‘ID’ – What column to use for ordering the categories. ‘order’ (string) default is ‘ASC’ – What direction to order categories. ‘show_last_update’ (bool|int) default is 0 – See get_categories() ‘show_count’ (bool|int) default is 0 – Whether to show how many posts are in the category. ‘hide_empty’ (bool|int) default is 1 – Whether to hide categories that don’t have any posts attached to them. ‘child_of’ (int) default is 0 – See get_categories(). ‘exclude’ (string) – See get_categories(). ‘echo’ (bool|int) default is 1 – Whether to display or retrieve content. ‘depth’ (int) – The max depth. ‘tab_index’ (int) – Tab index for select element. ‘name’ (string) – The name attribute value for select element. ‘id’ (string) – The ID attribute value for select element. Defaults to name if omitted. ‘class’ (string) – The class attribute value for select element. ‘selected’ (int) – Which category ID is selected. ‘taxonomy’ (string) – The name of the taxonomy to retrieve. Defaults to category.

Parameters

  • string|array $args: Optional. Override default arguments.

Return values

returns:HTML content only if ‘echo’ argument is 0.

Defined filters

  • list_cats
    apply_filters( 'list_cats', $show_option_none )
  • list_cats
    apply_filters( 'list_cats', $show_option_all )
  • list_cats
    apply_filters( 'list_cats', $show_option_none )
  • wp_dropdown_cats
    apply_filters( 'wp_dropdown_cats', $output )

Source code

function wp_dropdown_categories( $args = '' ) {

	$defaults = array(

		'show_option_all' => '', 'show_option_none' => '',

		'orderby' => 'id', 'order' => 'ASC',

		'show_last_update' => 0, 'show_count' => 0,

		'hide_empty' => 1, 'child_of' => 0,

		'exclude' => '', 'echo' => 1,

		'selected' => 0, 'hierarchical' => 0,

		'name' => 'cat', 'id' => '',

		'class' => 'postform', 'depth' => 0,

		'tab_index' => 0, 'taxonomy' => 'category',

		'hide_if_empty' => false

	);



	$defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;



	// Back compat.

	if ( isset( $args['type'] ) && 'link' == $args['type'] ) {

		_deprecated_argument( __FUNCTION__, '3.0', '' );

		$args['taxonomy'] = 'link_category';

	}



	$r = wp_parse_args( $args, $defaults );



	if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {

		$r['pad_counts'] = true;

	}



	$r['include_last_update_time'] = $r['show_last_update'];

	extract( $r );



	$tab_index_attribute = '';

	if ( (int) $tab_index > 0 )

		$tab_index_attribute = " tabindex=\"$tab_index\"";



	$categories = get_terms( $taxonomy, $r );

	$name = esc_attr( $name );

	$class = esc_attr( $class );

	$id = $id ? esc_attr( $id ) : $name;



	if ( ! $r['hide_if_empty'] || ! empty($categories) )

		$output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";

	else

		$output = '';



	if ( empty($categories) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {

		$show_option_none = apply_filters( 'list_cats', $show_option_none );

		$output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";

	}



	if ( ! empty( $categories ) ) {



		if ( $show_option_all ) {

			$show_option_all = apply_filters( 'list_cats', $show_option_all );

			$selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';

			$output .= "\t<option value='0'$selected>$show_option_all</option>\n";

		}



		if ( $show_option_none ) {

			$show_option_none = apply_filters( 'list_cats', $show_option_none );

			$selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" : '';

			$output .= "\t<option value='-1'$selected>$show_option_none</option>\n";

		}



		if ( $hierarchical )

			$depth = $r['depth'];  // Walk the full depth.

		else

			$depth = -1; // Flat.



		$output .= walk_category_dropdown_tree( $categories, $depth, $r );

	}

	if ( ! $r['hide_if_empty'] || ! empty($categories) )

		$output .= "</select>\n";





	$output = apply_filters( 'wp_dropdown_cats', $output );



	if ( $echo )

		echo $output;



	return $output;

}

3611

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: