wp_dropdown_categories() – Displays or retrieves the HTML dropdown list of categories.

You appear to be a bot. Output may be restricted

Description

Displays or retrieves the HTML dropdown list of categories.

The 'hierarchical' argument, which is disabled by default, will override the depth argument, unless it is true. When the argument is false, it will display all of the categories. When it is enabled it will use the value in the 'depth' argument.

Usage

$string = wp_dropdown_categories( $args );

Parameters

$args
( array|string ) optional – { Optional. Array or string of arguments to generate a categories drop-down element. See WP_Term_Query::__construct() for information on additional accepted arguments.
$show_option_all
( string ) optional – Text to display for showing all categories. Default empty.
$show_option_none
( string ) optional – Text to display for showing no categories. Default empty.
$option_none_value
( string ) optional – Value to use when no category is selected. Default empty.
$orderby
( string ) optional – Which column to use for ordering categories. See get_terms() for a list of accepted values. Default 'id' (term_id).
$pad_counts
( bool ) optional – See get_terms() for an argument description. Default false.
$show_count
( bool|int ) optional – Whether to include post counts. Accepts 0, 1, or their bool equivalents. Default 0.
$echo
( bool|int ) optional – Whether to echo or return the generated markup. Accepts 0, 1, or their bool equivalents. Default 1.
$hierarchical
( bool|int ) optional – Whether to traverse the taxonomy hierarchy. Accepts 0, 1, or their bool equivalents. Default 0.
$depth
( int ) optional – Maximum depth. Default 0.
$tab_index
( int ) optional – Tab index for the select element. Default 0 (no tabindex).
$name
( string ) optional – Value for the 'name' attribute of the select element. Default 'cat'.
$id
( string ) optional – Value for the 'id' attribute of the select element. Defaults to the value of `$name`.
$class
( string ) optional – Value for the 'class' attribute of the select element. Default 'postform'.
$selected
( int|string ) optional – Value of the option that should be selected. Default 0.
$value_field
( string ) optional – Term field that should be used to populate the 'value' attribute of the option elements. Accepts any valid term field: 'term_id', 'name', 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', 'parent', 'count'. Default 'term_id'.
$taxonomy
( string|array ) optional – Name of the taxonomy or taxonomies to retrieve. Default 'category'.
$hide_if_empty
( bool ) optional – True to skip generating markup if no categories are found. Default false (create select element even if no categories are found).
$required
( bool ) optional – Whether the <select> element should have the HTML5 'required' attribute. Default false.
$walker
( Walker ) optional – Walker object to use to build the output. Default empty which results in a Walker_CategoryDropdown instance being used.
$aria_describedby
( string ) optional – The 'id' of an element that contains descriptive text for the select. Default empty string. }

Returns

string HTML dropdown list of categories.

Source

File name: wordpress/wp-includes/category-template.php
Lines:

1 to 100 of 143
function wp_dropdown_categories( $args = '' ) {
  $defaults = array(
    'show_option_all'   => '',
    'show_option_none'  => '',
    'orderby'           => 'id',
    'order'             => 'ASC',
    '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,
    'option_none_value' => -1,
    'value_field'       => 'term_id',
    'required'          => false,
    'aria_describedby'  => '',
  );

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

  // Back compat.
  if ( isset( $args['type'] ) && 'link' === $args['type'] ) {
    _deprecated_argument(
      wp_dropdown_categories,
      '3.0.0',
      sprintf(
        /* translators: 1: "type => link", 2: "taxonomy => link_category" */
        __( '%1$s is deprecated. Use %2$s instead.' ),
        '<code>type => link</code>',
        '<code>taxonomy => link_category</code>'
      )
    );
    $args['taxonomy'] = 'link_category';
  }

  // Parse incoming $args into an array and merge it with $defaults.
  $parsed_args = wp_parse_args( $args, $defaults );

  $option_none_value = $parsed_args['option_none_value'];

  if ( ! isset( $parsed_args['pad_counts'] ) && $parsed_args['show_count'] && $parsed_args['hierarchical'] ) {
    $parsed_args['pad_counts'] = true;
  }

  $tab_index = $parsed_args['tab_index'];

  $tab_index_attribute = '';
  if ( (int) $tab_index > 0 ) {
    $tab_index_attribute = " tabindex=\"$tab_index\"";
  }

  // Avoid clashes with the 'name' param of get_terms().
  $get_terms_args = $parsed_args;
  unset( $get_terms_args['name'] );
  $categories = get_terms( $get_terms_args );

  $name     = esc_attr( $parsed_args['name'] );
  $class    = esc_attr( $parsed_args['class'] );
  $id       = $parsed_args['id'] ? esc_attr( $parsed_args['id'] ) : $name;
  $required = $parsed_args['required'] ? 'required' : '';

  $aria_describedby_attribute = $parsed_args['aria_describedby'] ? ' aria-describedby="' . esc_attr( $parsed_args['aria_describedby'] ) . '"' : '';

  if ( ! $parsed_args['hide_if_empty'] || ! empty( $categories ) ) {
    $output = "<select $required name='$name' id='$id' class='$class'$tab_index_attribute$aria_describedby_attribute>\n";
  } else {
    $output = '';
  }
  if ( empty( $categories ) && ! $parsed_args['hide_if_empty'] && ! empty( $parsed_args['show_option_none'] ) ) {

    
/**
 * Filters a taxonomy drop-down display element.
 *
 * A variety of taxonomy drop-down display elements can be modified
 * just prior to display via this filter. Filterable arguments include
 * 'show_option_none', 'show_option_all', and various forms of the
 * term name.
 *
 * @since 1.2.0
 *
 * @see wp_dropdown_categories()
 *
 * @param string       $element  Category name.
 * @param WP_Term|null $category The category object, or null if there's no corresponding category.
 */
    $show_option_none = apply_filters( 'list_cats', $parsed_args['show_option_none'], null );
    $output          .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n";
  }

  if ( ! empty( $categories ) ) {

    if ( $parsed_args['show_option_all'] ) {
 

 View on GitHub View on Trac