wpseek.com
A WordPress-centric search engine for devs and theme authors



parent_dropdown › WordPress Function

Since1.5.0
Deprecatedn/a
parent_dropdown ( $default_page = 0, $parent_page = 0, $level = 0, $post = null )
Parameters: (4)
  • (int) $default_page Optional. The default page ID to be pre-selected. Default 0.
    Required: No
    Default:
  • (int) $parent_page Optional. The parent page ID. Default 0.
    Required: No
    Default:
  • (int) $level Optional. Page depth level. Default 0.
    Required: No
    Default:
  • (int|WP_Post) $post Post ID or WP_Post object.
    Required: No
    Default: null
Returns:
  • (void|false) Void on success, false if the page has no children.
Defined at:
Codex:
Change Log:
  • 4.4.0

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



Source

function parent_dropdown( $default_page = 0, $parent_page = 0, $level = 0, $post = null ) {
	global $wpdb;

	$post  = get_post( $post );
	$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_page
		)
	);

	if ( $items ) {
		foreach ( $items as $item ) {
			// A page cannot be its own parent.
			if ( $post && $post->ID && (int) $item->ID === $post->ID ) {
				continue;
			}

			$pad      = str_repeat( ' ', $level * 3 );
			$selected = selected( $default_page, $item->ID, false );

			echo "\n\t<option class='level-$level' value='$item->ID' $selected>$pad " . esc_html( $item->post_title ) . '</option>';
			parent_dropdown( $default_page, $item->ID, $level + 1 );
		}
	} else {
		return false;
	}
}