get_the_content_feed()
云策文档标注
概述
get_the_content_feed() 函数用于在 RSS、Atom 等订阅源中检索并过滤文章内容。它通过应用 the_content 和 the_content_feed 过滤器来生成适合订阅源格式的内容。
关键要点
- 函数返回字符串类型的过滤后内容,适用于订阅源显示。
- 参数 $feed_type 可选,指定订阅源类型(如 'rss2'、'atom'),默认为 null,会使用 get_default_feed() 获取默认类型。
- 内部先应用 the_content 过滤器处理原始内容,再替换 ']]>' 字符,最后应用 the_content_feed 过滤器进行订阅源特定过滤。
- 相关 Hook 包括 the_content 和 the_content_feed,可用于自定义内容处理。
代码示例
function get_the_content_feed( $feed_type = null ) {
if ( ! $feed_type ) {
$feed_type = get_default_feed();
}
/** This filter is documented in wp-includes/post-template.php */
$content = apply_filters( 'the_content', get_the_content() );
$content = str_replace( ']]>', ']]>', $content );
/**
* Filters the post content for use in feeds.
*
* @since 2.9.0
*
* @param string $content The current post content.
* @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'.
* Default 'rss2'.
*/
return apply_filters( 'the_content_feed', $content, $feed_type );
}注意事项
- 函数自 WordPress 2.9.0 版本引入,是订阅源内容处理的核心函数。
- 使用时需确保在循环内调用,以获取当前文章的内容。
- 可通过 the_content_feed 过滤器调整输出,适应不同订阅源格式需求。
原文内容
Retrieves the post content for feeds.
Description
See also
Parameters
$feed_typestringoptional-
The type of feed. rss2 | atom | rss | rdf
Default:
null
Source
function get_the_content_feed( $feed_type = null ) {
if ( ! $feed_type ) {
$feed_type = get_default_feed();
}
/** This filter is documented in wp-includes/post-template.php */
$content = apply_filters( 'the_content', get_the_content() );
$content = str_replace( ']]>', ']]>', $content );
/**
* Filters the post content for use in feeds.
*
* @since 2.9.0
*
* @param string $content The current post content.
* @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'.
* Default 'rss2'.
*/
return apply_filters( 'the_content_feed', $content, $feed_type );
}
Hooks
- apply_filters( ‘the_content’, string $content )
-
Filters the post content.
- apply_filters( ‘the_content_feed’, string $content, string $feed_type )
-
Filters the post content for use in feeds.
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |