类文档

Walker_CategoryDropdown

💡 云策文档标注

概述

Walker_CategoryDropdown 是 WordPress 核心类,用于生成分类的 HTML 下拉列表。它继承自 Walker 类,专门处理分类树结构,通过 start_el 方法构建下拉选项。

关键要点

  • 继承自 Walker 类,用于处理分类(category)树结构,生成下拉列表。
  • 定义了 $tree_type 为 'category' 和 $db_fields 数组,指定数据库字段映射。
  • 核心方法 start_el 负责输出每个下拉选项,支持参数如 selected、show_count 和 value_field。
  • 方法内部使用过滤器 list_cats 修改分类名称,并处理选项选中状态和计数显示。

代码示例

public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
    $category = $data_object;
    $pad = str_repeat( ' ', $depth * 3 );
    $cat_name = apply_filters( 'list_cats', $category->name, $category );
    if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) {
        $value_field = $args['value_field'];
    } else {
        $value_field = 'term_id';
    }
    $output .= "	{$value_field} ) . '"";
    if ( (string) $category->{$value_field} === (string) $args['selected'] ) {
        $output .= ' selected="selected"';
    }
    $output .= '>';
    $output .= $pad . $cat_name;
    if ( $args['show_count'] ) {
        $output .= '  (' . number_format_i18n( $category->count ) . ')';
    }
    $output .= "n";
}

注意事项

  • 从 WordPress 5.9.0 开始,start_el 方法的参数 $category 和 $id 更名为 $data_object 和 $current_object_id,以支持 PHP 8 命名参数。
  • 使用 $value_field 参数可自定义选项值字段,默认为 term_id。
  • 通过 $args['selected'] 设置选中项,需进行字符串类型转换以避免误匹配。
  • 相关类 Walker 位于 wp-includes/class-wp-walker.php,用于显示树状结构。

📄 原文内容

Core class used to create an HTML dropdown list of Categories.

Description

See also

Methods

Name Description
Walker_CategoryDropdown::start_el Starts the element output.

Source

class Walker_CategoryDropdown extends Walker {

	/**
	 * What the class handles.
	 *
	 * @since 2.1.0
	 * @var string
	 *
	 * @see Walker::$tree_type
	 */
	public $tree_type = 'category';

	/**
	 * Database fields to use.
	 *
	 * @since 2.1.0
	 * @todo Decouple this
	 * @var string[]
	 *
	 * @see Walker::$db_fields
	 */
	public $db_fields = array(
		'parent' => 'parent',
		'id'     => 'term_id',
	);

	/**
	 * Starts the element output.
	 *
	 * @since 2.1.0
	 * @since 5.9.0 Renamed `$category` 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_Term $data_object       Category data object.
	 * @param int     $depth             Depth of category. Used for padding.
	 * @param array   $args              Uses 'selected', 'show_count', and 'value_field' keys, if they exist.
	 *                                   See wp_dropdown_categories().
	 * @param int     $current_object_id Optional. ID of the current category. 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.
		$category = $data_object;

		$pad = str_repeat( ' ', $depth * 3 );

		/** This filter is documented in wp-includes/category-template.php */
		$cat_name = apply_filters( 'list_cats', $category->name, $category );

		if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) {
			$value_field = $args['value_field'];
		} else {
			$value_field = 'term_id';
		}

		$output .= "t<option class="level-$depth" value="" . esc_attr( $category->{$value_field} ) . '"';

		// Type-juggling causes false matches, so we force everything to a string.
		if ( (string) $category->{$value_field} === (string) $args['selected'] ) {
			$output .= ' selected="selected"';
		}
		$output .= '>';
		$output .= $pad . $cat_name;
		if ( $args['show_count'] ) {
			$output .= '  (' . number_format_i18n( $category->count ) . ')';
		}
		$output .= "</option>n";
	}
}

Changelog

Version Description
2.1.0 Introduced.