Walker_PageDropdown
云策文档标注
概述
Walker_PageDropdown 是 WordPress 核心类,用于生成页面下拉列表的 HTML 结构。它继承自 Walker 类,专门处理页面数据,通过 start_el 方法构建选项元素。
关键要点
- 继承自 Walker 类,用于树状结构显示,专门处理页面类型(tree_type = 'page')。
- 定义数据库字段映射(db_fields),如 parent 对应 post_parent,id 对应 ID。
- 核心方法 start_el 负责输出每个选项元素,支持深度缩进、选中状态和自定义值字段。
- 使用 list_pages 过滤器允许开发者修改页面标题在列表中的显示。
- 自 WordPress 2.1.0 引入,5.9.0 更新参数命名以支持 PHP 8 命名参数。
代码示例
public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
$page = $data_object;
$pad = str_repeat( ' ', $depth * 3 );
if ( ! isset( $args['value_field'] ) || ! isset( $page->{$args['value_field']} ) ) {
$args['value_field'] = 'ID';
}
$output .= " <option class="level-$depth" value="" . esc_attr( $page->{$args['value_field']} ) . '"';
if ( $page->ID === (int) $args['selected'] ) {
$output .= ' selected="selected"';
}
$output .= '>';
$title = $page->post_title;
if ( '' === $title ) {
$title = sprintf( __( '#%d (no title)' ), $page->ID );
}
$title = apply_filters( 'list_pages', $title, $page );
$output .= $pad . esc_html( $title );
$output .= "n";
}注意事项
- start_el 方法参数在 5.9.0 版本重命名(如 $page 改为 $data_object),以匹配父类并支持 PHP 8 命名参数,但内部仍使用 $page 变量保持可读性。
- 默认使用页面 ID 作为选项值,但可通过 args['value_field'] 自定义,需确保字段存在于页面对象中。
- 输出时自动处理标题转义和空标题情况,并应用 list_pages 过滤器进行定制。
原文内容
Core class used to create an HTML drop-down list of pages.
Description
See also
Methods
| Name | Description |
|---|---|
| Walker_PageDropdown::start_el | Starts the element output. |
Source
class Walker_PageDropdown extends Walker {
/**
* What the class handles.
*
* @since 2.1.0
* @var string
*
* @see Walker::$tree_type
*/
public $tree_type = 'page';
/**
* Database fields to use.
*
* @since 2.1.0
* @var string[]
*
* @see Walker::$db_fields
* @todo Decouple this
*/
public $db_fields = array(
'parent' => 'post_parent',
'id' => 'ID',
);
/**
* Starts the element output.
*
* @since 2.1.0
* @since 5.9.0 Renamed `$page` to `$data_object` and `$id` to `$current_object_id`
* to match parent class for PHP 8 named parameter support.
*
* @see Walker::start_el()
*
* @param string $output Used to append additional content. Passed by reference.
* @param WP_Post $data_object Page data object.
* @param int $depth Optional. Depth of page in reference to parent pages.
* Used for padding. Default 0.
* @param array $args Optional. Uses 'selected' argument for selected page to
* set selected HTML attribute for option element. Uses
* 'value_field' argument to fill "value" attribute.
* See wp_dropdown_pages(). Default empty array.
* @param int $current_object_id Optional. ID of the current page. Default 0.
*/
public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
// Restores the more descriptive, specific name for use within this method.
$page = $data_object;
$pad = str_repeat( ' ', $depth * 3 );
if ( ! isset( $args['value_field'] ) || ! isset( $page->{$args['value_field']} ) ) {
$args['value_field'] = 'ID';
}
$output .= "t<option class="level-$depth" value="" . esc_attr( $page->{$args['value_field']} ) . '"';
if ( $page->ID === (int) $args['selected'] ) {
$output .= ' selected="selected"';
}
$output .= '>';
$title = $page->post_title;
if ( '' === $title ) {
/* translators: %d: ID of a post. */
$title = sprintf( __( '#%d (no title)' ), $page->ID );
}
/**
* Filters the page title when creating an HTML drop-down list of pages.
*
* @since 3.1.0
*
* @param string $title Page title.
* @param WP_Post $page Page data object.
*/
$title = apply_filters( 'list_pages', $title, $page );
$output .= $pad . esc_html( $title );
$output .= "</option>n";
}
}
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |