1. 首页
  2. WordPress 函数手册

wp_dropdown_pages()

wp_dropdown_pages( array|string $args =  )

以下拉列表形式检索或显示页面列表(选择列表)。
Retrieve or display a list of pages as a dropdown (select list).

目录锚点:#说明#源码#笔记


说明(Description)

另请参见get#pages()


源码(Source)

/**
 * Retrieve or display list of pages as a dropdown (select list).
 *
 * @since 2.1.0
 * @since 4.2.0 The `$value_field` argument was added.
 * @since 4.3.0 The `$class` argument was added.
 *
 * @param array|string $args {
 *     Optional. Array or string of arguments to generate a pages drop-down element.
 *
 *     @type int          $depth                 Maximum depth. Default 0.
 *     @type int          $child_of              Page ID to retrieve child pages of. Default 0.
 *     @type int|string   $selected              Value of the option that should be selected. Default 0.
 *     @type bool|int     $echo                  Whether to echo or return the generated markup. Accepts 0, 1,
 *                                               or their bool equivalents. Default 1.
 *     @type string       $name                  Value for the 'name' attribute of the select element.
 *                                               Default 'page_id'.
 *     @type string       $id                    Value for the 'id' attribute of the select element.
 *     @type string       $class                 Value for the 'class' attribute of the select element. Default: none.
 *                                               Defaults to the value of `$name`.
 *     @type string       $show_option_none      Text to display for showing no pages. Default empty (does not display).
 *     @type string       $show_option_no_change Text to display for "no change" option. Default empty (does not display).
 *     @type string       $option_none_value     Value to use when no page is selected. Default empty.
 *     @type string       $value_field           Post field used to populate the 'value' attribute of the option
 *                                               elements. Accepts any valid post field. Default 'ID'.
 * }
 * @return string HTML content, if not displaying.
 */
function wp_dropdown_pages( $args = '' ) {
	$defaults = array(
		'depth' => 0, 'child_of' => 0,
		'selected' => 0, 'echo' => 1,
		'name' => 'page_id', 'id' => '',
		'class' => '',
		'show_option_none' => '', 'show_option_no_change' => '',
		'option_none_value' => '',
		'value_field' => 'ID',
	);

	$r = wp_parse_args( $args, $defaults );

	$pages = get_pages( $r );
	$output = '';
	// Back-compat with old system where both id and name were based on $name argument
	if ( empty( $r['id'] ) ) {
		$r['id'] = $r['name'];
	}

	if ( ! empty( $pages ) ) {
		$class = '';
		if ( ! empty( $r['class'] ) ) {
			$class = " class='" . esc_attr( $r['class'] ) . "'";
		}

		$output = "
";
		if ( $r['show_option_no_change'] ) {
			$output .= "	" . $r['show_option_no_change'] . "
";
		}
		if ( $r['show_option_none'] ) {
			$output .= "	' . $r['show_option_none'] . "
";
		}
		$output .= walk_page_dropdown_tree( $pages, $r['depth'], $r );
		$output .= "
";
	}

	/**
	 * Filter the HTML output of a list of pages as a drop down.
	 *
	 * @since 2.1.0
	 *
	 * @param string $output HTML output for drop down list of pages.
	 */
	$html = apply_filters( 'wp_dropdown_pages', $output );

	if ( $r['echo'] ) {
		echo $html;
	}
	return $html;
}
更新版本 源码位置 使用 被使用
4.3.0 wp-includes/post-template.php 3 19

笔记(Notes)

此外,$args可以包括“sort_column”和其他get_pages()参数,如page_attributes_meta_box()源代码所示:

wp_dropdown_pages() 为WP2原创文章,链接:https://www.wp2.cn/functions/wp_dropdown_pages/