the_category()
云策文档标注
概述
the_category() 是 WordPress 模板标签,用于显示文章的分类列表,支持 HTML 列表或自定义分隔符格式。它基于 get_the_category_list() 函数实现,直接输出分类链接。
关键要点
- 功能:输出文章的分类列表,默认以无序列表形式显示。
- 参数:$separator 控制分隔符,空字符串使用默认列表;$parents 控制父分类显示方式,可选 'multiple'、'single' 或空字符串;$post_id 指定文章 ID,默认为当前文章。
- 实现:直接调用 get_the_category_list() 并输出结果,无返回值。
- 版本:自 WordPress 0.71 版本引入。
代码示例
function the_category( $separator = '', $parents = '', $post_id = false ) {
echo get_the_category_list( $separator, $parents, $post_id );
}注意事项
- $parents 参数默认为空字符串,但未明确其默认行为是 'multiple' 还是 'single',使用时需注意。
- 使用自定义分隔符(如逗号、箭头、空格或项目符号)时,需考虑可读性和语义,避免误导用户。
原文内容
Displays category list for a post in either HTML list or custom format.
Parameters
$separatorstringoptional-
Separator between the categories. By default, the links are placed in an unordered list. An empty string will result in the default behavior.
$parentsstringoptional-
How to display the parents. Accepts
'multiple','single', or empty.
Default empty string. $post_idint|falseoptional-
ID of the post to retrieve categories for. Defaults to the current post.
Default:
false
Source
function the_category( $separator = '', $parents = '', $post_id = false ) {
echo get_the_category_list( $separator, $parents, $post_id );
}
Changelog
| Version | Description |
|---|---|
| 0.71 | Introduced. |
Skip to note 6 content
Codex
Separated by Space
List categories with a space as the separator.
Skip to note 7 content
oliverdrummond
$parents
▪ ‘multiple’ – Display separate links to parent and child categories, exhibiting “parent/child” relationship. (Exhibiting the parents and child with links pointing for both)
▪ ‘single’ – Display link to child category only, with link text exhibiting “parent/child” relationship. (Exhibiting the parents and child but the link points only to the child category)
Skip to note 8 content
Codex
Separated by Comma
Displays links to categories, each category separated by a comma (if more than one).
Skip to note 9 content
Codex
Separated by Arrow
Displays links to categories with an arrow (>) separating the categories. Note: Take care when using this, since some viewers may interpret a category following a > as a subcategory of the one preceding it.
Skip to note 10 content
Codex
Separated by a Bullet
Displays links to categories with a bullet (•) separating the categories.