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
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;
}
Skip to note 4 content
Elías
If you want to filter the description, don’t use this function, use
get_the_archive_description()instead. This one doesapply_filtersbefore returning the description, and usesterm_description()internally to retrieve the description when is for a term.Skip to note 5 content
Codex
Displays a description of the post tag ID 28.
Skip to note 6 content
Codex
The default usage returns the description of the current queried term.