函数文档

the_content_rss()

💡 云策文档标注

概述

the_content_rss() 是一个已弃用的 WordPress 函数,用于在 RSS 订阅中显示文章内容。它支持通过参数控制内容编码和字数限制,但自 2.9.0 版本起被 the_content_feed() 替代。

关键要点

  • 函数已弃用:自 WordPress 2.9.0 起,推荐使用 the_content_feed() 替代。
  • 参数功能:$more_link_text 设置更多内容链接文本,$cut 限制字数,$encode_html 控制 HTML 编码方式(0 为链接脚注,1 为转义特殊字符,2 为去除所有 HTML 标签)。
  • 编码与字数限制:如果设置了 $cut 但未设置 $encode_html,则 $encode_html 默认值为 2,会去除所有 HTML 标签。
  • 相关函数:涉及 get_the_content()、esc_html()、make_url_footnote() 等辅助函数。
  • Hook:提供 apply_filters('the_content_rss', $content) 用于过滤 RSS 内容。

代码示例

function the_content_rss($more_link_text='(more...)', $stripteaser=0, $more_file='', $cut = 0, $encode_html = 0) {
    _deprecated_function( __FUNCTION__, '2.9.0', 'the_content_feed()' );
    $content = get_the_content($more_link_text, $stripteaser);
    $content = apply_filters('the_content_rss', $content);
    if ( $cut && !$encode_html )
        $encode_html = 2;
    if ( 1== $encode_html ) {
        $content = esc_html($content);
        $cut = 0;
    } elseif ( 0 == $encode_html ) {
        $content = make_url_footnote($content);
    } elseif ( 2 == $encode_html ) {
        $content = strip_tags($content);
    }
    if ( $cut ) {
        $blah = explode(' ', $content);
        if ( count($blah) > $cut ) {
            $k = $cut;
            $use_dotdotdot = 1;
        } else {
            $k = count($blah);
            $use_dotdotdot = 0;
        }
        for ( $i=0; $i<$k; $i++ )
            $excerpt .= $blah[$i] . ' ';
        $excerpt .= ($use_dotdotdot) ? '...' : '';
        $content = $excerpt;
    }
    $content = str_replace(']]>', ']]>', $content);
    echo $content;
}

注意事项

  • 弃用警告:使用此函数会触发 _deprecated_function(),建议更新代码以避免未来兼容性问题。
  • 参数交互:注意 $cut 和 $encode_html 的默认行为,不当设置可能导致内容显示异常。

📄 原文内容

Display the post content for the feed.

Description

For encoding the HTML or the $encode_html parameter, there are three possible values:

  • ‘0’ will make urls footnotes and use make_url_footnote() .
  • ‘1’ will encode special characters and automatically display all of the content.
  • ‘2’ will strip all HTML tags from the content.

Also note that you cannot set the amount of words and not set the HTML encoding.
If that is the case, then the HTML encoding will default to 2, which will strip all HTML tags.

To restrict the amount of words of the content, you can use the cut parameter.
If the content is less than the amount, then there won’t be any dots added to the end.
If there is content left over, then dots will be added and the rest of the content will be removed.

See also

Parameters

$more_link_textstringoptional
Text to display when more content is available but not displayed. Default '(more...)'.
$stripteaserintoptional
Default 0.
$more_filestringoptional
$cutintoptional
Amount of words to keep for the content.
$encode_htmlintoptional
How to encode the content.

Source

function the_content_rss($more_link_text='(more...)', $stripteaser=0, $more_file='', $cut = 0, $encode_html = 0) {
	_deprecated_function( __FUNCTION__, '2.9.0', 'the_content_feed()' );
	$content = get_the_content($more_link_text, $stripteaser);

	/**
	 * Filters the post content in the context of an RSS feed.
	 *
	 * @since 0.71
	 *
	 * @param string $content Content of the current post.
	 */
	$content = apply_filters('the_content_rss', $content);
	if ( $cut && !$encode_html )
		$encode_html = 2;
	if ( 1== $encode_html ) {
		$content = esc_html($content);
		$cut = 0;
	} elseif ( 0 == $encode_html ) {
		$content = make_url_footnote($content);
	} elseif ( 2 == $encode_html ) {
		$content = strip_tags($content);
	}
	if ( $cut ) {
		$blah = explode(' ', $content);
		if ( count($blah) > $cut ) {
			$k = $cut;
			$use_dotdotdot = 1;
		} else {
			$k = count($blah);
			$use_dotdotdot = 0;
		}

		/** @todo Check performance, might be faster to use array slice instead. */
		for ( $i=0; $i<$k; $i++ )
			$excerpt .= $blah[$i].' ';
		$excerpt .= ($use_dotdotdot) ? '...' : '';
		$content = $excerpt;
	}
	$content = str_replace(']]>', ']]>', $content);
	echo $content;
}

Hooks

apply_filters( ‘the_content_rss’, string $content )

Filters the post content in the context of an RSS feed.

Changelog

Version Description
2.9.0 Deprecated. Use the_content_feed()
0.71 Introduced.