描述:
获取所有的页面,并创建一个下拉菜单
用法:
<?php wp_dropdown_pages( $args ); ?>
默认用法:
<?php $args = array(
'depth' => 0,
'child_of' => 0,
'selected' => 0,
'echo' => 1,
'name' => 'page_id',
'id' => null, // string
'class' => null, // string
'show_option_none' => null, // string
'show_option_no_change' => null, // string
'option_none_value' => null, // string
); ?>
参数:
depth
(integer) 此参数控制页面层次结构中要包含在wp_list_pages生成的列表中的级别。默认值为0(显示所有页面,包括所有子页面)。
- 0 - 以分层(缩进)形式显示的页和子页(默认)。
- -1 - 无分层(无缩进)形式显示的子页中的页。
- 1 - 只展示顶级页面
- 2 - 值2(或更大)指定显示页面时要下降的深度(或级别)。
child_of
(integer) 仅显示单个页的子页;将页的ID用作值。默认为0(显示所有页面)。
selected
(integer) 要“选定”或显示在显示框中的页的页ID。默认为未选择页面。
echo
(boolean) 切换生成的链接列表的显示,或将该列表作为要在PHP中使用的HTML文本字符串返回。默认值为1(显示生成的列表项)。有效值:
- 1 (true) - default
- 0 (false)
name
(string) 指定给下拉表单的名称。默认为“页面id”。
id
(string) 分配给下拉表单的Id。默认为名称。
class
(string) 指定给下拉列表的类。默认为空。
show_option_none
(string) 使下拉列表的HTML不允许您选择任何页面。如果不为空,则此字符串将用作选项文本。
option_none_value
(string) 将“显示选项无”作为其文本的选项的值属性。
show_option_no_change
(string) 使下拉列表的HTML允许您选择值为-1的无更改选项。
exclude
(string) 要排除的页ID的逗号分隔列表。例如,“exclude=4,12”表示不会显示/回显或返回页面id 4和12。默认为不排除任何内容。
exclude_tree
(string) 定义要排除的父页ID。使用此参数可排除父页和该父页的所有子页。因此'exclude_tree=5'将排除父页5及其子页。此参数在版本2.7中可用。
示例:
<li id="pages">
<h2><?php _e('pages:'); ?></h2>
<form action="<?php bloginfo('url'); ?>" method="get">
<?php wp_dropdown_pages(); ?>
<input type="submit" name="submit" value="view" />
</form>
</li>
源文件:
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',
);
$parsed_args = wp_parse_args( $args, $defaults );
$pages = get_pages( $parsed_args );
$output = '';
// Back-compat with old system where both id and name were based on $name argument
if ( empty( $parsed_args['id'] ) ) {
$parsed_args['id'] = $parsed_args['name'];
}
if ( ! empty( $pages ) ) {
$class = '';
if ( ! empty( $parsed_args['class'] ) ) {
$class = " class='" . esc_attr( $parsed_args['class'] ) . "'";
}
$output = "<select name='" . esc_attr( $parsed_args['name'] ) . "'" . $class . " id='" . esc_attr( $parsed_args['id'] ) . "'>\n";
if ( $parsed_args['show_option_no_change'] ) {
$output .= "\t<option value=\"-1\">" . $parsed_args['show_option_no_change'] . "</option>\n";
}
if ( $parsed_args['show_option_none'] ) {
$output .= "\t<option value=\"" . esc_attr( $parsed_args['option_none_value'] ) . '">' . $parsed_args['show_option_none'] . "</option>\n";
}
$output .= walk_page_dropdown_tree( $pages, $parsed_args['depth'], $parsed_args );
$output .= "</select>\n";
}
/**
* Filters the HTML output of a list of pages as a drop down.
*
* @since 2.1.0
* @since 4.4.0 `$parsed_args` and `$pages` added as arguments.
*
* @param string $output HTML output for drop down list of pages.
* @param array $parsed_args The parsed arguments array.
* @param array $pages List of WP_Post objects returned by `get_pages()`
*/
$html = apply_filters( 'wp_dropdown_pages', $output, $parsed_args, $pages );
if ( $parsed_args['echo'] ) {
echo $html;
}
return $html;
}