函数文档

the_excerpt_rss()

💡 云策文档标注

概述

the_excerpt_rss() 函数用于在 RSS 或 Atom 等订阅源中显示文章摘要。它通过 get_the_excerpt() 获取摘要,并应用 the_excerpt_rss 过滤器进行自定义。

关键要点

  • 函数 the_excerpt_rss() 输出文章摘要,适用于订阅源显示。
  • 内部调用 get_the_excerpt() 获取摘要,并通过 apply_filters() 应用 the_excerpt_rss 过滤器。
  • 开发者可以使用 the_excerpt_rss 过滤器钩子修改输出内容。
  • 函数自 WordPress 0.71 版本引入,历史悠久。

代码示例

if ( isset( $_GET['type'] ) ) {
    $typewanted = sanitize_text_field( $_GET['type'] );
}

if ( $typewanted == 'excerpt' ) {
    // Wrap the Excerpt in a span tag for CSS styling
    echo '<span class="excerpt">'; 
        the_excerpt_rss();   
    echo '</span>';
} else {  
    // Otherwise they want the full content so wrap it in another span
    echo '<span class="content">';
        the_content();
    echo '</span>';
}

注意事项

  • 示例代码中使用了 $_GET 参数和 sanitize_text_field() 进行安全处理,确保在自定义订阅源中安全使用。
  • 注意代码中的变量名拼写错误(如 $typewantd),实际使用时需修正为 $typewanted。

📄 原文内容

Displays the post excerpt for the feed.

Source

function the_excerpt_rss() {
	$output = get_the_excerpt();
	/**
	 * Filters the post excerpt for a feed.
	 *
	 * @since 1.2.0
	 *
	 * @param string $output The current post excerpt.
	 */
	echo apply_filters( 'the_excerpt_rss', $output );
}

Hooks

apply_filters( ‘the_excerpt_rss’, string $output )

Filters the post excerpt for a feed.

Changelog

Version Description
0.71 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example

    To create a custom feed that takes a GET parameter on a URL (e.g. http://www.example.com/?feed=myfeed&type;=excerpt), place something like the following in your particular feed file to send an excerpt (the_excerpt_rss() ) instead of the full content (the_content()):

    if isset( $_GET['type'] ) ) {
    	$typewanted = sanitize_text_field( $_GET['type'] );
    }
    
    if ( $typewantd == 'excerpt' ) {
    	// Wrap the Excerpt in a span tag for CSS styling
    	echo '<span class="excerpt">'; 
    		the_excerpt_rss();   
    	echo '</span>';
    } else {  
    	// Otherwise they want the full content so wrap it in another span
    	echo '<span class="content">';
    		the_content();
    	echo '</span>';
    }