Wordpress:向rss Feed添加内容

时间:2016-06-29 07:26:53

标签: wordpress rss

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

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时,我没有在自定义函数中返回的测试内容。

有人知道为什么吗?

1 个答案:

答案 0 :(得分:2)

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

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

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

function customRSSFunc(){
        get_template_part('rss', 'feedname');
}

上面的代码使用get_template_part函数链接到单独的模板文件,但您也可以将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

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