parent_dropdown

Print out option HTML elements for the page parents drop-down.

Description

(null|false) parent_dropdown( (int) $default = 0, (int) $parent = 0, (int) $level = 0, (null) $post = null ); 

Returns (null|false)

Boolean False if page has no children, otherwise print out html elements

Parameters (4)

0. $default — Optional. (int)
The default page ID to be pre-selected. Default 0.
1. $parent — Optional. (int)
The parent page ID. Default 0.
2. $level — Optional. (int)
Page depth level. Default 0.
3. $post — Optional. (null) => null
Post ID or WP_Post object.

Usage

  1. if ( !function_exists( 'parent_dropdown' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/template.php'; 
  3.  
  4. // Optional. The default page ID to be pre-selected. Default 0. 
  5. $default = -1; 
  6.  
  7. // Optional. The parent page ID. Default 0. 
  8. $parent = -1; 
  9.  
  10. // Optional. Page depth level. Default 0. 
  11. $level = -1; 
  12.  
  13. // Post ID or WP_Post object. 
  14. $post = null; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = parent_dropdown($default, $parent, $level, $post); 
  18.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/template.php  
  1. function parent_dropdown( $default = 0, $parent = 0, $level = 0, $post = null ) { 
  2. global $wpdb; 
  3. $post = get_post( $post ); 
  4. $items = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_parent, post_title FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'page' ORDER BY menu_order", $parent) ); 
  5.  
  6. if ( $items ) { 
  7. foreach ( $items as $item ) { 
  8. // A page cannot be its own parent. 
  9. if ( $post && $post->ID && $item->ID == $post->ID ) 
  10. continue; 
  11.  
  12. $pad = str_repeat( ' ', $level * 3 ); 
  13. $selected = selected( $default, $item->ID, false ); 
  14.  
  15. echo "\n\t<option class='level-$level' value='$item->ID' $selected>$pad " . esc_html($item->post_title) . "</option>"; 
  16. parent_dropdown( $default, $item->ID, $level +1 ); 
  17. } else { 
  18. return false;