Retrieves or displays a list of pages as a dropdown (select list).
$argsarray|stringoptional
depth intchild_of intselected int|stringecho bool|intname string'name' attribute of the select element.'page_id'.id string'id' attribute of the select element.class string'class' attribute of the select element. Default: none.$name.show_option_none stringshow_option_no_change stringoption_none_value stringvalue_field string'value' attribute of the option elements. Accepts any valid post field. Default 'ID'.child_of int$hierarchical has no bearing on whether $child_of returns hierarchical results. Default 0, or no restriction.sort_order string'ASC', 'DESC'. Default 'ASC'.sort_column string'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.'post_title'.hierarchical bool$child_of also being false, both arguments will be disregarded.exclude int[]include int[]$child_of, $parent, $exclude, $meta_key, $meta_value, or $hierarchical.meta_key stringmeta_value string$meta_key.authors stringparent intexclude_tree string|int[]number intoffset int$number.post_type string'page'.post_status string|array'publish'.Default:''
function wp_dropdown_pages( $args = '' ) {
$defaults = array(
'depth' => 0,
'child_of' => 0,
'selected' => 0,
'echo' => 1,
'name' => 'page_id',
'id' => '',
'class' => '',
'show_option_none' => '',
'show_option_no_change' => '',
'option_none_value' => '',
'value_field' => 'ID',
);
$parsed_args = wp_parse_args( $args, $defaults );
$pages = get_pages( $parsed_args );
$output = '';
// Back-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( $pages ) ) {
$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_change'] ) {
$output .= "\t<option value=\"-1\">" . $parsed_args['show_option_no_change'] . "</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 .= walk_page_dropdown_tree( $pages, $parsed_args['depth'], $parsed_args );
$output .= "</select>\n";
}
/**
* Filters the HTML output of a list of pages as a dropdown.
*
* @since 2.1.0
* @since 4.4.0 `$parsed_args` and `$pages` added as arguments.
*
* @param string $output HTML output for dropdown list of pages.
* @param array $parsed_args The parsed arguments array. See wp_dropdown_pages()
* for information on accepted arguments.
* @param WP_Post[] $pages Array of the page objects.
*/
$html = apply_filters( 'wp_dropdown_pages', $output, $parsed_args, $pages );
if ( $parsed_args['echo'] ) {
echo $html;
}
return $html;
}
Filters the HTML output of a list of pages as a dropdown.
© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_dropdown_pages