category_description()
云策文档标注
概述
category_description() 函数用于检索分类的描述文本,基于分类 ID 或当前分类。在 archive.php 模板中使用时需结合 is_category() 条件语句以避免影响其他归档页面。
关键要点
- 参数 $category 可选,默认为 0,表示当前分类 ID
- 返回字符串类型的分类描述,若无描述则返回空字符串
- 在 archive.php 中必须包裹在 is_category() 条件内,否则可能中断月度归档等页面的处理
- 底层调用 term_description() 函数实现功能
代码示例
// 默认用法:显示当前分类的描述
echo category_description();
// 指定分类 ID 显示描述
echo category_description(5);
// 结合分类标题使用
echo '<h1>' . single_cat_title('', false) . '</h1>';
echo '<p>' . category_description() . '</p>';注意事项
- 若无分类描述,函数返回空字符串而非 br 标签(文档中用户笔记有误,实际返回空字符串)
- 可通过分类 slug 获取 term_id 再调用函数,如:echo category_description(get_category_by_slug('wordpress')->term_id);
原文内容
Retrieves category description.
Parameters
$categoryintoptional-
Category ID. Defaults to the current category ID.
Source
function category_description( $category = 0 ) {
return term_description( $category );
}
Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |
Skip to note 4 content
Codex
Default Usage
Displays the description of a category, given its id, by echoing the return value of the tag. If no category given and used on a category page, it returns the description of the current category.
<div></div>Result:
WordPress is a favorite blogging tool of mine and I share tips and tricks for using WordPress here.
Note: if there is no category description, the function returns a br tag.
Skip to note 5 content
Codex
Using Category Slug
Displays the description of a category, using a category slug.
term_id ); ?>Skip to note 6 content
Codex
With Category Title
<div> <strong></strong>: </div>Result:
[html]
Currently browsing WordPress: WordPress is a favorite blogging tool of mine and I share tips and tricks for using WordPress here.
[/html]