wp_dropdown_pages()获取所有的页面,并创建一个下拉菜单

目录

描述

Displays a list of pages in a select (i.e dropdown) box with no submit button.

wp_dropdown_pages() 描述

用法

 <?php wp_dropdown_pages$args ); ?> 

Default Usage

 <?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
); ?>

By default, the usage shows:

  • Pages and sub-pages displayed in hierarchical (indented) form
  • Displays all pages (not restricted to child pages)
  • No page is 'selected' or presented in the display box
  • The name assigned to the dropdown form is 'page_id'
  • Allows you to select NONE of the pages (show_option_none=>'' not shown in above example)
  • Exclude no pages (exclude=>'' not shown in above example)
  • Exclude no 'tree' (exclude_tree=>'' not shown in above example)

wp_dropdown_pages() 用法

参数

depth 

(integer)This parameter controls how many levels in the hierarchy of pages are to be included in the list generated by wp_list_pages. The default value is 0 (display all pages, including all sub-pages).

  • 0 - Pages and sub-pages displayed in hierarchical (indented) form (Default).
  • -1 - Pages in sub-pages displayed in flat (no indent) form.
  • 1 - Show only top level Pages
  • 2 - Value of 2 (or greater) specifies the depth (or level) to descend in displaying Pages.

child_of 

(integer) Displays the sub-pages of a single Page only; uses the ID for a Page as the value. Defaults to 0 (displays all Pages).

selected 

(integer) Page ID of the page to be 'selected' or presented in the display box. Defaults to no page selected.

echo 

(boolean) Toggles the display of the generated list of links or return the list as an HTML text string to be used in PHP. The default value is 1 (display the generated list items). Valid values:

  • 1 (true) - default
  • 0 (false)

name 

(string) Name assigned to the dropdown form. Defaults to 'page_id'.

id 

(string) Id assigned to the dropdown form. Defaults to the name.

class

(string) Class assigned to the dropdown form. Defaults empty.

show_option_none 

(string) Causes the HTML for the dropdown to allow you to select NONE of the pages. If not empty, this string will be used as the option text.

option_none_value 

(string) The value attribute for the option that has show_option_none as its text.

show_option_no_change 

(string) Causes the HTML for the dropdown to allow you to select a NO CHANGE option with value -1.

exclude 

(string) Comma separated list of page IDs to exclude. For example, 'exclude=4,12' means page IDs 4 and 12 will NOT be displayed/echoed or returned. Defaults to exclude nothing.

exclude_tree 

(string) Define a parent Page ID to be excluded. Use this parameter to exclude a parent and all of that parent's child Pages. So 'exclude_tree=5' would exclude the parent Page 5, and its child Pages. This parameter was available at Version 2.7.

wp_dropdown_pages() 参数

示例

Dropdown with Submit Button

Displays a hierarchical page dropdown list in HTML form with a submit button.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<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>

wp_dropdown_pages() 示例

历史

添加于 版本 2.1.0

wp_dropdown_pages() 历史

源文件

wp_dropdown_pages() 函数的代码位于 wp-includes/post-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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 = "
";
	}

	/**
	 * 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;
}

wp_dropdown_pages() 源文件

相关