parent_dropdown()
Gets a list of child pages of the specified parent page. Outputs an HTML list of <option> elements for the <select> element.
The result needs to be wrapped in a <select> tag.
Also outputs child pages for each obtained child page, if they exist. This recursion continues until the end, i.e., all the tree of child pages will be obtained...
The function works only with the post type page. If another post type is needed, use the function code to create your own similar function...
The function is defined only in the admin area. If needed on the front end, include this file:
require_once ABSPATH .'wp-admin/includes/template.php';
1 time — 0.001482 sec (very slow) | 50000 times — 52 sec (very slow)
No Hooks.
Returns
null|false. False, if there are no child pages. If there are, it will immediately output the HTML code to the screen.
Usage
parent_dropdown( $default, $parent, $level, $post );
- $default(int)
- ID of the page that will be selected in the list.
Default: 0 - $parent(int)
- ID of the parent page whose children need to be obtained.
Default: 0 - $level(int)
- Current level of <option> elements. If specified as 1, a visual indent will be added to the left of the elements. This is needed to embed elements into an already existing list. Usually, this parameter is not specified; it is used by the function itself in recursion.
Default: 0 - $post(int/WP_Post)
- ID of the page to be excluded from the list. By default, the current page is specified.
Default: null
Examples
#1 Display the dropdown list of child pages
<?php // to work in the front-end // require_once ABSPATH .'wp-admin/includes/template.php'; ?> <select name="my_page"> <?php parent_dropdown( 25, 280 ); ?> </select>
We get it:
<select name="my_page"> <option class='level-0' value='3484'>Daughter Page 1</option> <option class='level-0' value='1544'>Daughter Page 2</option> <option class='level-1' value='1787'>Daughter page 1, child page 2</option> <option class='level-0' value='3644'>Daughter Page 3</option> </select>
Notes
- Global. wpdb.
$wpdbWordPress database abstraction object.
Changelog
| Since 1.5.0 | Introduced. |
| Since 4.4.0 | $post argument was added. |