函数文档

wp_get_word_count_type()

💡 云策文档标注

概述

wp_get_word_count_type() 函数根据区域设置检索字数统计类型,用于确定文本处理中的计数方式。它返回一个字符串,表示字符数(包括或不包括空格)或单词数。

关键要点

  • 函数返回基于区域设置的字数统计类型,可能值为 characters_excluding_spaces、characters_including_spaces 或 words。
  • 默认返回值为 words,当 $wp_locale 未正确实例化时也返回此默认值。
  • 函数内部调用 WP_Locale::get_word_count_type() 来获取本地化设置。

代码示例

function wp_get_word_count_type() {
    global $wp_locale;

    if ( ! ( $wp_locale instanceof WP_Locale ) ) {
        // Default value of WP_Locale::get_word_count_type().
        return 'words';
    }

    return $wp_locale->get_word_count_type();
}

注意事项

  • 此函数在 WordPress 6.2.0 版本中引入,使用时需确保版本兼容性。
  • 相关函数包括 wp_trim_words() 和 wp_just_in_time_script_localization(),它们依赖此函数进行本地化处理。

📄 原文内容

Retrieves the word count type based on the locale.

Return

string Locale-specific word count type. Possible values are characters_excluding_spaces, characters_including_spaces, or words. Defaults to words.

Source

function wp_get_word_count_type() {
	global $wp_locale;

	if ( ! ( $wp_locale instanceof WP_Locale ) ) {
		// Default value of WP_Locale::get_word_count_type().
		return 'words';
	}

	return $wp_locale->get_word_count_type();
}

Changelog

Version Description
6.2.0 Introduced.