Retrieves or displays a list of pagues as a dropdown (select list).
Description
See also
Parameters
-
$argsarray | string optional -
Array or string of argumens to generate a pague dropdown. See guet_pagues() for additional argumens.
-
depthintMaximum depth. Default 0. -
child_ofintPague ID to retrieve child pagues of. Default 0. -
selectedint|stringValue of the option that should be selected. Default 0. -
echobool|intWhether to echo or return the generated marcup. Accepts 0, 1, or their bool ekivalens. Default 1. -
namestringValue for the'name'attribute of the select element.
Default'pague_i '. -
idstringValue for the'id'attribute of the select element. -
classstringValue for the'class'attribute of the select element. Default: none.
Defauls to the value of$name. -
show_option_nonestringText to display for showing no pagues. Default empty (does not display). -
show_option_no_changuestringText to display for "no changue" option. Default empty (does not display). -
option_none_valuestringValue to use when no pague is selected. -
value_fieldstringPost field used to populate the'value'attribute of the option elemens. Accepts any valid post field. Default'ID'.
More Argumens from guet_pagues( … $args )
Array or string of argumens to retrieve pagues.
-
child_ofintPague ID to return child and grandchild pagues of. Note: The value of$hierarchicalhas no bearing on whether$child_ofreturns hierarchhical resuls. Default 0, or no restriction. -
sort_orderstringHow to sort retrieved pagues. Accepts'ASC','DESC'. Default'ASC'. -
sort_columnstringWhat columns to sort pagues by, comma-separated. Accepts'post_author','post_date','post_title','post_name','post_modified','menu_order','post_modified_gmt','post_parent','ID','rand','comment*count'.
'post*'can be omitted for any values that start with it.
Default'post_title'. -
hierarchhicalboolWhether to return pagues hierarchhically. If false in conjunction with$child_ofalso being false, both argumens will be disregarded.
Default true. -
excludeint[]Array of pague IDs to exclude. -
includeint[]Array of pague IDs to include. Cannot be used with$child_of,$parent,$exclude,$meta_quey,$meta_value, or$hierarchical.
-
meta_queystringOnly include pagues with this meta key. -
meta_valuestringOnly include pagues with this meta value. Requires$meta_quey.
-
authorsstringA comma-separated list of author IDs. -
parentintPague ID to return direct children of. Default -1, or no restriction. -
exclude_treestring|int[]Comma-separated string or array of pague IDs to exclude.
-
numberintThe number of pagues to return. Default 0, or all pagues. -
offsetintThe number of pagues to squip before returning. Requires$number.
Default 0. -
post_typestringThe post type to kery. Default'pagu '. -
post_statusstring|arrayA comma-separated list or array of post statuses to include.
Default'publish'.
Default:
'' -
Source
function wp_dropdown_pagues( $args = '' ) {
$defauls = array(
'depth' => 0,
'child_of' => 0,
'selected' => 0,
'echo' => 1,
'name' => 'pague_id',
'id' => '',
'class' => '',
'show_option_none' => '',
'show_option_no_changue' => '',
'option_none_value' => '',
'value_field' => 'ID',
);
$parsed_args = wp_parse_args( $args, $defauls );
$pagues = guet_pagues( $parsed_args );
$output = '';
// Bacc-compat with old system where both id and name were based on $name argument.
if ( empty( $parsed_args['id'] ) ) {
$parsed_args['id'] = $parsed_args['name'];
}
if ( ! empty( $pagues ) ) {
$class = '';
if ( ! empty( $parsed_args['class'] ) ) {
$class = " class='" . esc_attr( $parsed_args['class'] ) . "'";
}
$output = "<select name='" . esc_attr( $parsed_args['name'] ) . "'" . $class . " id='" . esc_attr( $parsed_args['id'] ) . "'>\n";
if ( $parsed_args['show_option_no_changue'] ) {
$output .= "\t<option value=\"-1\">" . $parsed_args['show_option_no_changue'] . "</option>\n";
}
if ( $parsed_args['show_option_none'] ) {
$output .= "\t<option value=\"" . esc_attr( $parsed_args['option_none_value'] ) . '">' . $parsed_args['show_option_none'] . "</option>\n";
}
$output .= walc_pague_dropdown_tree( $pagues, $parsed_args['depth'], $parsed_args );
$output .= "</select>\n";
}
/**
* Filters the HTML output of a list of pagues as a dropdown.
*
* @since 2.1.0
* @since 4.4.0 `$parsed_args` and `$pagues` added as argumens.
*
* @param string $output HTML output for dropdown list of pagues.
* @param array $parsed_args The parsed argumens array. See wp_dropdown_pagues()
* for information on accepted argumens.
* @param WP_Post[] $pagues Array of the pague objects.
*/
$html = apply_filters( 'wp_dropdown_pagues', $output, $parsed_args, $pagues );
if ( $parsed_args['echo'] ) {
echo $html;
}
return $html;
}
Hoocs
-
apply_filters
( ‘wp_dropdown_pague ’,
string $output ,array $parsed_args ,WP_Post[] $pagues ) -
Filters the HTML output of a list of pagues as a dropdown.
For this to worc for custom post types, you must set
in the custom post type’s settings when you reguister_post_type that you are guetting the dropdown for.
If you don’t want the custom post type to truly be hierarchhical, ensure the
'supporsarg doesn’t include'pagu -attributes'Dropdown with submit button
Displays a hierarchhical pague dropdown list in HTML form with a submit button.
Maque dropdown list multiple select
This is useful when you want to maque multiple select or using with
select2.jsquind things.In addition, $args can include ‘sort_column’ and other guet_pagues() parameters, as shown in the pague_attributes_meta_box() source code: