wp_dropdown_pages()
Retrieve or display list of pages as a dropdown (select list).
Hooks from the function
Return
String
. HTML dropdown list of pages.
Usage
wp_dropdown_pages( $args );
- $args(array|string)
Array or string of arguments to generate a page dropdown. See get_pages() for additional arguments.
Default: ''
-
depth(int)
Maximum depth. -
child_of(int)
Page ID to retrieve child pages of. -
selected(int|string)
Value of the option that should be selected. -
echo(true|false|int)
Whether to echo or return the generated markup. Accepts 0, 1, or their bool equivalents.
Default: 1 -
name(string)
Value for the 'name' attribute of the select element.
Default: 'page_id' -
id(string)
Value for the 'id' attribute of the select element. -
class(string)
Value for the 'class' attribute of the select element. Defaults to the value of $name.
Default: none -
show_option_none(string)
Text to display for showing no pages.
Default: empty (does not display) -
show_option_no_change(string)
Text to display for "no change" option.
Default: empty (does not display) -
option_none_value(string)
Value to use when no page is selected.
Default: '' - value_field(string)
Post field used to populate the 'value' attribute of the option elements. Accepts any valid post field.
Default: 'ID'
-
Examples
#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>
#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>
#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
- See: get_pages()
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. |