get_the_category_rss()
云策文档标注
概述
get_the_category_rss() 函数用于在 WordPress 中获取当前文章的所有分类,并格式化为适合在 RSS、Atom 等订阅源中使用的标记。它支持多种订阅源类型,并自动处理分类和标签的清理与输出。
关键要点
- 函数返回当前文章的分类列表,格式化为订阅源兼容的字符串,适用于 RSS2、Atom、RSS1 和 RSS0.91 RDF 等类型。
- 参数 $type 可选,指定订阅源类型,默认为 get_default_feed() 返回的值;函数内部会根据类型调整过滤器和输出格式。
- 函数内部调用 get_the_category() 和 get_the_tags() 获取分类和标签,使用 sanitize_term_field() 进行清理,确保输出安全。
- 输出结果可通过 apply_filters('the_category_rss', $the_list, $type) 钩子进行过滤,方便开发者自定义。
代码示例
function get_the_category_rss( $type = null ) {
if ( empty( $type ) ) {
$type = get_default_feed();
}
$categories = get_the_category();
$tags = get_the_tags();
$the_list = '';
$cat_names = array();
$filter = 'rss';
if ( 'atom' === $type ) {
$filter = 'raw';
}
if ( ! empty( $categories ) ) {
foreach ( (array) $categories as $category ) {
$cat_names[] = sanitize_term_field( 'name', $category->name, $category->term_id, 'category', $filter );
}
}
if ( ! empty( $tags ) ) {
foreach ( (array) $tags as $tag ) {
$cat_names[] = sanitize_term_field( 'name', $tag->name, $tag->term_id, 'post_tag', $filter );
}
}
$cat_names = array_unique( $cat_names );
foreach ( $cat_names as $cat_name ) {
if ( 'rdf' === $type ) {
$the_list .= "tt<dc:subject><![CDATA[" . $cat_name . "]]></dc:subject>n";
} elseif ( 'atom' === $type ) {
$the_list .= sprintf( '<category scheme="%s" term="%s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
} else {
$the_list .= "tt<category><![CDATA[" . $cat_name . "]]></category>n";
}
}
return apply_filters( 'the_category_rss', $the_list, $type );
}注意事项
- 函数在 WordPress 2.1.0 版本中引入,使用时需确保环境兼容。
- 输出内容已通过 esc_attr() 等函数进行转义,但开发者仍应遵循安全最佳实践,避免直接输出未过滤的数据。
- 相关函数包括 get_the_tags()、get_the_category()、sanitize_term_field() 等,可用于扩展功能或调试。
原文内容
Retrieves all of the post categories, formatted for use in feeds.
Description
All of the categories for the current post in the feed loop, will be retrieved and have feed markup added, so that they can easily be added to the RSS2, Atom, or RSS1 and RSS0.91 RDF feeds.
Parameters
$typestringoptional-
Default is the type returned by get_default_feed() .
Default:
null
Source
function get_the_category_rss( $type = null ) {
if ( empty( $type ) ) {
$type = get_default_feed();
}
$categories = get_the_category();
$tags = get_the_tags();
$the_list = '';
$cat_names = array();
$filter = 'rss';
if ( 'atom' === $type ) {
$filter = 'raw';
}
if ( ! empty( $categories ) ) {
foreach ( (array) $categories as $category ) {
$cat_names[] = sanitize_term_field( 'name', $category->name, $category->term_id, 'category', $filter );
}
}
if ( ! empty( $tags ) ) {
foreach ( (array) $tags as $tag ) {
$cat_names[] = sanitize_term_field( 'name', $tag->name, $tag->term_id, 'post_tag', $filter );
}
}
$cat_names = array_unique( $cat_names );
foreach ( $cat_names as $cat_name ) {
if ( 'rdf' === $type ) {
$the_list .= "tt<dc:subject></dc:subject>n";
} elseif ( 'atom' === $type ) {
$the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( get_bloginfo_rss( 'url' ) ), esc_attr( $cat_name ) );
} else {
$the_list .= "tt<category></category>n";
}
}
/**
* Filters all of the post categories for display in a feed.
*
* @since 1.2.0
*
* @param string $the_list All of the RSS post categories.
* @param string $type Type of feed. Possible values include 'rss2', 'atom'.
* Default 'rss2'.
*/
return apply_filters( 'the_category_rss', $the_list, $type );
}
Hooks
- apply_filters( ‘the_category_rss’, string $the_list, string $type )
-
Filters all of the post categories for display in a feed.
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |