函数文档

term_description()

💡 云策文档标注

概述

term_description() 是 WordPress 2.8 引入的模板标签,用于检索指定术语的描述。如果未提供术语 ID,则返回当前查询术语的描述,输出会自动包裹在 <p> 标签中。

关键要点

  • 函数返回术语描述字符串,若不可用则返回空字符串
  • 参数 $term 可选,默认为 0,表示使用当前查询术语 ID
  • 参数 $deprecated 已弃用,保留为 null
  • 内部调用 get_term_field() 获取描述,并检查是否为 WP_Error
  • 适用于分类、标签和自定义分类法术语

代码示例

// 显示标签 ID 28 的描述
echo term_description( 28 );

注意事项

  • 若需过滤描述,建议使用 get_the_archive_description(),而非此函数
  • get_the_archive_description() 内部使用 term_description(),但仅适用于存档页面循环
  • 从版本 4.9.2 起,$taxonomy 参数已弃用

📄 原文内容

Retrieves term description.

Parameters

$termintoptional
Term ID. Defaults to the current term ID.
$deprecatednulloptional
Deprecated. Not used.

Default:null

Return

string Term description, if available.

More Information

First available with WordPress Version 2.8, this template tag returns the description of a given term. A term ID and taxonomy are as parameters. If no term ID is passed, the description of current queried term (e.g. post category or tag) will be returned.

Output is wrapped in

tags.

Source

function term_description( $term = 0, $deprecated = null ) {
	if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) {
		$term = get_queried_object();
		if ( $term ) {
			$term = $term->term_id;
		}
	}

	$description = get_term_field( 'description', $term );

	return is_wp_error( $description ) ? '' : $description;
}

Changelog

Version Description
4.9.2 The $taxonomy parameter was deprecated.
2.8.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    If you want to filter the description, don’t use this function, use get_the_archive_description() instead. This one does apply_filters before returning the description, and uses term_description() internally to retrieve the description when is for a term.