函数文档

wp_count_terms()

💡 云策文档标注

概述

wp_count_terms() 函数用于统计指定分类法中的术语数量,支持通过参数控制是否包含空术语,并兼容旧版函数签名。

关键要点

  • 函数返回指定分类法中术语的数量,以数字字符串形式表示,若分类法不存在则返回 WP_Error。
  • 参数 $args 可以是数组或字符串,默认包含 'hide_empty' => false,用于控制是否隐藏空术语。
  • 支持向后兼容:如果第一个参数是分类法名称或数组,则视为旧版签名,第二个参数作为 $args 处理。
  • 内部通过调用 get_terms() 并设置 'fields' => 'count' 来实现计数功能。

代码示例

function wp_count_terms( $args = array(), $deprecated = '' ) {
    $use_legacy_args = false;

    // Check whether function is used with legacy signature: `$taxonomy` and `$args`.
    if ( $args
        && ( is_string( $args ) && taxonomy_exists( $args )
            || is_array( $args ) && wp_is_numeric_array( $args ) )
    ) {
        $use_legacy_args = true;
    }

    $defaults = array( 'hide_empty' => false );

    if ( $use_legacy_args ) {
        $defaults['taxonomy'] = $args;
        $args                 = $deprecated;
    }

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

    // Backward compatibility.
    if ( isset( $args['ignore_empty'] ) ) {
        $args['hide_empty'] = $args['ignore_empty'];
        unset( $args['ignore_empty'] );
    }

    $args['fields'] = 'count';

    return get_terms( $args );
}

注意事项

  • 从 WordPress 5.6.0 开始,函数签名已更改,允许 $args 数组作为第一个参数,开发者应更新代码以适应新签名。
  • 参数 $deprecated 仅用于向后兼容,新代码中应避免使用,直接传递 $args 数组。
  • 函数内部处理了 'ignore_empty' 到 'hide_empty' 的兼容性转换,确保旧参数仍能正常工作。

📄 原文内容

Counts how many terms are in taxonomy.

Description

Default $args is ‘hide_empty’ which can be ‘hide_empty=true’ or array(‘hide_empty’ => true).

{@internal The $deprecated parameter is parsed for backward compatibility only.}

Parameters

$argsarray|stringoptional
Array or string of arguments. See WP_Term_Query::__construct() for information on accepted arguments.

Default:array()

$deprecatedarray|stringoptional
Argument array, when using the legacy function parameter format.
If present, this parameter will be interpreted as $args, and the first function parameter will be parsed as a taxonomy or array of taxonomies.
Default empty.

Return

string|WP_Error Numeric string containing the number of terms in that taxonomy or WP_Error if the taxonomy does not exist.

Source

function wp_count_terms( $args = array(), $deprecated = '' ) {
	$use_legacy_args = false;

	// Check whether function is used with legacy signature: `$taxonomy` and `$args`.
	if ( $args
		&& ( is_string( $args ) && taxonomy_exists( $args )
			|| is_array( $args ) && wp_is_numeric_array( $args ) )
	) {
		$use_legacy_args = true;
	}

	$defaults = array( 'hide_empty' => false );

	if ( $use_legacy_args ) {
		$defaults['taxonomy'] = $args;
		$args                 = $deprecated;
	}

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

	// Backward compatibility.
	if ( isset( $args['ignore_empty'] ) ) {
		$args['hide_empty'] = $args['ignore_empty'];
		unset( $args['ignore_empty'] );
	}

	$args['fields'] = 'count';

	return get_terms( $args );
}

Changelog

Version Description
5.6.0 Changed the function signature so that the $args array can be provided as the first parameter.
2.3.0 Introduced.