Using WordPress ‘parent_dropdown()’ PHP function

The parent_dropdown() WordPress PHP function prints out option HTML elements for the page parents drop-down.

Usage

To use the parent_dropdown() function, you can call it with optional parameters to customize the output. Here’s an example:

parent_dropdown(5, 0, 0, $post);

This will generate a drop-down menu with the default page pre-selected as page ID 5.

Parameters

  • $default_page (int) Optional: The default page ID to be pre-selected. Default 0.
  • $parent_page (int) Optional: The parent page ID. Default 0.
  • $level (int) Optional: Page depth level. Default 0.
  • $post (int|WP_Post) Optional: Post ID or WP_Post object. Default: null.

More information

See WordPress Developer Resources: parent_dropdown()

Examples

Basic usage of parent_dropdown

Generate a default drop-down menu for selecting a parent page.

parent_dropdown();

Pre-select a specific page

Generate a drop-down menu with a specific page pre-selected by ID.

parent_dropdown(12);

Display only children of a specific page

Generate a drop-down menu displaying only children of a specific parent page.

parent_dropdown(0, 8);

Set the depth level of pages

Generate a drop-down menu with a specific depth level.

parent_dropdown(0, 0, 2);

Use a WP_Post object as the last parameter

Generate a drop-down menu using a WP_Post object.

$post = get_post(15);
parent_dropdown(0, 0, 0, $post);