admin管理员组

文章数量:1122832

The problem: I have a custom post type "announcements" and a custom taxonomy "announcements_taxonomy". I can access the RSS feed by going to - The problem is I can not see the categories/tags in the RSS feed.

Detail: I am using Simplepie to get the feed items and show them outside of a WordPress install. Because the RSS feed on my WP site does not show the categories I can not access them with the get_categories() function.

Question: How can I get categories from my custom post type and taxonomy to show in the RSS feed?

The problem: I have a custom post type "announcements" and a custom taxonomy "announcements_taxonomy". I can access the RSS feed by going to http://domain.com/announcements/feed - The problem is I can not see the categories/tags in the RSS feed.

Detail: I am using Simplepie to get the feed items and show them outside of a WordPress install. Because the RSS feed on my WP site does not show the categories I can not access them with the get_categories() function.

Question: How can I get categories from my custom post type and taxonomy to show in the RSS feed?

Share Improve this question edited Aug 15, 2022 at 10:44 bueltge 17.1k7 gold badges61 silver badges97 bronze badges asked Dec 19, 2017 at 14:19 DerekDerek 3732 gold badges6 silver badges17 bronze badges 3
  • 1 Is the RSS feed your only option? What about using the REST API instead? That would expose whatever you like to your external site. – WebElaine Commented Dec 19, 2017 at 14:26
  • I suppose not but I have not worked with the WP Rest API yet so the RSS Feed was my go to option – Derek Commented Dec 19, 2017 at 15:24
  • For anyone looking to do this with the REST API, here is a nice URL for a beginners guide to how to use it for this test case: css-tricks.com/using-the-wp-api-to-fetch-posts – Derek Commented Dec 19, 2017 at 20:12
Add a comment  | 

1 Answer 1

Reset to default 0

Here is how to show taxonomy terms in rss feed. Just add this to the functions.php file and update the $tax and $post_type variables to match your needs.:

add_filter('the_category_rss', 'wpq_the_category_rss');
add_filter('rss2_ns', 'wpq_rss2_ns');


function wpq_the_category_rss($the_list_original)
{
$tax = 'announcement_tags';
$post_type = 'announcements';

if(get_post_type() != $post_type)
return $the_list;

$categories = get_the_terms(get_the_ID(), $tax);

$the_list = '';
$cat_names = array();

if(!empty($categories))
{
foreach((array)$categories as $category)
{
$cat_names[] = sanitize_term_field('name', $category->name, $category->term_id, $tax, 'rss');
$cat_descriptions[] = sanitize_term_field('description', $category->description, $category->term_id, $tax, 'rss');
}
}

$cat_names = array_unique($cat_names);
$cat_descriptions = array_unique($cat_descriptions);

foreach($cat_names as $cat_name)
{
$the_list .= "\t\t<category domain=\"". $tax ."\"><![CDATA[" . @html_entity_decode($cat_name, ENT_COMPAT, get_option('blog_charset') ) . "]]></category>\n";
}

foreach($cat_descriptions as $cat_description)
{
$the_list .= "\t\t<wpq:category_description domain=\"". $tax ."\"><![CDATA[". @html_entity_decode($cat_description, ENT_COMPAT, get_option('blog_charset')) ."]]></wpq:category_description>\n";
}

return $the_list_original.$the_list;
}

本文标签: Showing Taxonomy Terms in RSS Feed