钩子文档

rss2_item

💡 云策文档标注

概述

rss2_item 是一个 WordPress 动作钩子,在每个 RSS2 源项目输出后触发,用于向 RSS2 源添加自定义内容。它不依赖主题,可在 functions.php 或插件中使用。

关键要点

  • 触发时机:在每个 RSS2 源项目的默认输出后立即触发
  • 参数与返回值:无参数,无返回值,主要用于输出数据到源中
  • 使用场景:可添加自定义元素,如图像或自定义字段,但需注意可能影响源的有效性
  • 兼容性:从 WordPress 2.0.0 版本引入

代码示例

add_action( 'rss2_item', 'wporg_add_my_rss_node' );

function wporg_add_my_rss_node() {
	global $post;
	if ( has_post_thumbnail( $post->ID ) ) :
		$thumbnail = get_attachment_link( get_post_thumbnail_id( $post->ID ) );
		echo "{$thumbnail}";
	endif;
}

注意事项

  • 添加自定义元素(如非标准标签)可能导致 RSS2 源不符合规范,影响验证和兼容性
  • 建议使用 Enclosure 等标准方法添加媒体,需提供长度和 MIME 类型等详细信息
  • 用户贡献的代码示例仅供参考,实际使用时需评估其对源有效性的影响

📄 原文内容

Fires at the end of each RSS2 feed item.

More Information

The rss2_item action hook is triggered immediately after the default output of an rss2 feed.

This hook has no parameters and no return values. It can be used to echo data into the feed if necessary.

This hook is not dependent on your theme. It can be called within your theme’s functions.php file or from a plugin.

Source

do_action( 'rss2_item' );

Changelog

Version Description
2.0.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Add an image element for each post

    Adds an image node to your RSS2 feeds

    add_action( 'rss2_item', 'wporg_add_my_rss_node' );
    
    function wporg_add_my_rss_node() {
    	global $post;
    	if ( has_post_thumbnail( $post->ID ) ) :
    		$thumbnail = get_attachment_link( get_post_thumbnail_id( $post->ID ) );
    		echo "<image>{$thumbnail}</image>";
    	endif;
    }

  2. Skip to note 4 content

    (From Codex)
    Add a custom field element for each post
    Adds a node from a custom field to your RSS2 feeds using get_post_meta() .

    Note: Custom elements like the one in this example are not a part of the RSS 2.0 specification, so adding it will mean your feed will technically be invalid RSS 2.0. Few, if any, RSS readers will make any use of the custom element.

    ID, 'my-custom-field', true);
    	if(!empty($metaValue)):
    		echo("<my-custom-field>{$metaValue}</my-custom-field>");
    	endif;
    }
    ?>