Wordpress:将内容添加到 rss 提要

Wordpress : add content to rss feed

我正在尝试向 WordPress RSS 提要添加一些自定义值。出于测试目的,我得到了这段代码:

function add_custom_fields_to_rss() {
    return "<test>test</test>\n";
}
add_action('rss_item', 'add_custom_fields_to_rss');
add_action('rss_item2', 'add_custom_fields_to_rss');

我已将它放在主题的底部 function.php。当我现在尝试使用 http://example.com/feed 获取 rss 时,我的自定义函数中没有返回任何测试内容。

有人知道为什么吗?

您需要做的第一件事是在主题的 functions.php 文件中创建新的 RSS 提要

add_action('init', 'customRSS');
function customRSS(){
        add_feed('feedname', 'customRSSFunc');
}

以上代码触发了 customRSS 函数,该函数添加了提要。 add_feed 函数有两个参数,feedname 和一个回调函数。 feedname 将构成您的新 feed url yourdomain.com/feed/feedname 并且回调函数将被调用以实际创建 feed。记下提要名称,因为您稍后会需要它。 初始化提要后,您需要创建回调函数以生成所需的提要,使用主题 functions.php 文件

中的以下代码
function customRSSFunc(){
        get_template_part('rss', 'feedname');
}

上面的代码使用get_template_part 函数将link 到一个单独的模板文件,但是您也可以将RSS 代码直接放入函数中。通过使用 get_template_part,我们可以将功能与布局分开。 get_template_part 函数有两个参数,slug 和 name,它将查找具有以下格式名称的模板文件,从顶部的文件开始(如果找不到第一个,它将移动到第二个,依此类推):

wp-content/themes/child/rss-feedname.php
wp-content/themes/parent/rss-feedname.php
wp-content/themes/child/rss.php
wp-content/themes/parent/rss.php

有关详细信息,您应该查看此 link http://www.wpbeginner.com/wp-tutorials/how-to-create-custom-rss-feeds-in-wordpress/