wp_dropdown_pages()WP 2.1.0

Outputs a dropdown list of static pages (list in the <select> tag).

Hooks from the function

Returns

String.

Usage Template

$args = array(
	'depth'            => 0,
	'child_of'         => 0,
	'selected'         => 0,
	'echo'             => 1,
	'name'             => 'page_id',
	'show_option_none' => '',
	'exclude'          => '',
	'exclude_tree'     => '',
	'value_field'      => 'ID', // field for the value attribute in the option tag
);

wp_dropdown_pages( $args );

Usage

wp_dropdown_pages( $args );
$args(string/array)

Array of arguments to modify. Can be specified as a query string.

Any parameters that the get_pages() function accepts can be specified.
Default: array of default arguments

Arguments of the $args parameter

depth(int)

This parameter controls the depth of nested pages (how many nested pages will be shown). Default is 0 - all levels of child pages will be shown.

  • 0 - all levels of child pages will be shown. Default;

  • -1 - parent and child pages will be shown in a flat list (without indentation from the left edge);

  • 1 - show only first-level static pages;

  • 2 - 2, 3, 4, 5 ... these values indicate how deep to show pages.
child_of(int)
Show only child pages of the specified. You need to specify the ID of the parent page.
selected(int)
ID of the static page to be highlighted in the list (specify the selected attribute).
Default: 0
echo(boolean)
Output to screen (1) or return for processing (0). By default, it outputs to screen.
Default: 1
name(string)
Value of the name attribute of the HTML <select> tag.
Default: 'page_id'
show_option_none(string)
Allows setting the select field choice to an empty value. The text for the empty value is specified in this parameter.
Default: ''
exclude(string/int)
ID of static pages to exclude from the list. Specify as a comma-separated list.
Default: ''
exclude_tree(int/string)
ID of parent pages whose tree should not be shown (including the page itself). That is, if you specify, for example, 45, then the page 45 and all its subpages will be absent from the list.
Default: ''
value_field(string)
Field whose value will be used for the value attribute in the option tag. Can be the name of any field in the posts table (ID, post_title, post_name).
Default: 'ID'

get_pages() Parameters

In addition to the above, you can also use parameters designed for the get_pages() function.

List of parameters for get_pages() not mentioned here:

'sort_order'   => 'ASC',
'sort_column'  => 'post_title',
'hierarchical' => 1,
'include'      => '',
'meta_key'     => '',
'meta_value'   => '',
'authors'      => ''

Examples

0

#1 Demo

<?php wp_dropdown_pages(); ?>

We get it:

<select name='page_id' id='page_id'>
	<option class="level-0" value="760">Page 1</option>
	<option class="level-0" value="280">Page 2</option>
	<option class="level-1" value="1544"> Subsidiary page 1</option>
	<option class="level-1" value="3484"> Subsidiary page 2</option>
	<option class="level-1" value="3644"> Subsidiary page 3</option>
	<option class="level-0" value="4970">Page 3</option>
	<option class="level-0" value="7">Page 4</option>
</select>
0

#2 An example of a dropdown list of static pages with a submit button:

<li id="pages">

	<h2><?php _e('pages:'); ?></h2>

	<form action="<?php bloginfo('url'); ?>" method="get">
	<?php wp_dropdown_pages(); ?>
	<input type="submit" name="submit" value="view" />
	</form>

</li>
0

#3 Make dropdown list multiple select

This is useful when you want to make multiple select or using with select2.js kinda things.

add_filter( 'wp_dropdown_pages', 'wporg_domain_make_multiple_select_pages' );

function wporg_domain_make_multiple_select_pages( $output ) {
	return str_replace( '<select ', '<select multiple="multiple" ', $output );
}

Notes

Changelog

Since 2.1.0 Introduced.
Since 4.2.0 The $value_field argument was added.
Since 4.3.0 The $class argument was added.

wp_dropdown_pages() code WP 7.0

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;
}