Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

how to add description of custom taxonomy term to RSS Feed WordPress

  • SOLVED

I have a multisite install with one main site and 3 sub-sites together forming a network.

On the sub-site:
For a Custom Post Type called "Review" I have set up a couple of Custom Taxonomies. One of them is (author)"Rating" (a category-like taxonomy) and the 5 different terms are 1 star, 2 stars, 3 stars, 4 stars and 5 stars. These terms also have a description and for that description I use star images.
So far so good.

On the main site:
In the sidebar I want to show the 5 most recent Reviews of each of the 3 sub-sites. That's pretty easy by adding the feed of each of these 3 sub-sites into a widget. But what I so far have been unable to accomplish is to get the description of the terms of the Rating taxonomy to show up next to it.

For that I obviously first need to add the description of the terms of the custom taxonomy to the feed and then I need to extract it again from the feed.

Anyone that can help me with this, would be very much appreciated! If you need a better explanation, please let me know. I don't think having the url is necessary to solve this problem; if needed I can only give via private message.

Answers (3)

2011-10-27

Jurre Hanema answers:

To add the terms of a custom taxonomy to an RSS feed, try this:


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 = 'product_cat';
$post_type = 'product';

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;
}


function wpq_rss2_ns()
{
echo "xmlns:wpq=\"http://www.wpquestions.com/question/show/id/3246/\"\n";
}


As you can see in the function wpq_the_category_rss(), this function works for posts of type 'product' and for the taxonomy 'product_cat'. You can of course change these two to your liking.

The code adds a new element <wpq:category_description> containing the description of the term for each term the post is associated with. (I invented the wpq-namespace to keep the RSS valid and all).

Do you think this is enough or do you need more help with reading this data back from the feed? (if so, please post the code you are using now)


senlin comments:

Hi Jurre,
Your code sample works perfect and indeed adds the new element to the feed!
Yes, it would be really helpful if you can help with reading this data back from the feed too.
What I am using now to call the feed is a RSS widget (on the main site) where I use the feed-address of:
sub-site.domain.com/reviews/feed/
The output of the RSS now shows:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:wpq="http://www.wpquestions.com/question/show/id/3246/"
>

<channel>
<title>name of subsite &#187; Reviews</title>
<atom:link href="http://sub-site.domain.com/reviews/feed/" rel="self" type="application/rss+xml" />
<link>http://sub-site.domain.com</link>
<description>description sub site</description>

<lastBuildDate>Thu, 27 Oct 2011 07:28:20 +0000</lastBuildDate>
<language>en</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
<item>

<title>Name Review</title>
<link>http://sub-site.domain.com/review/name-review/</link>
<comments>http://sub-site.domain.com/review/name-review/#comments</comments>
<pubDate>Mon, 12 Sep 2011 13:04:42 +0000</pubDate>
<dc:creator>Piet</dc:creator>
<category domain="rating"><![CDATA[4 stars]]></category>

<wpq:category_description domain="rating"><![CDATA[<img src="http://domain.com/wp-content/plugins/wp-postratings/images/stars(png)/rating_on.png" /><img src="http://domain.com/wp-content/plugins/wp-postratings/images/stars(png)/rating_on.png" /><img src="http://domain.com/wp-content/plugins/wp-postratings/images/stars(png)/rating_on.png" /><img src="http://domain.com/wp-content/plugins/wp-postratings/images/stars(png)/rating_on.png" /><img src="http://domain.com/wp-content/plugins/wp-postratings/images/stars(png)/rating_off.png" />]]></wpq:category_description>

<guid isPermaLink="false">http://sub-site.domain.com/?post_type=review&#038;p=72</guid>
<description><![CDATA[<p>If you enjoy poker at all this game is fun, quick and addicting.</p><p><a href="http://sub-site.domain.com/review/name-review/">title review</a> has originally been posted on <a href="http://sub-site.domain.com">name subsite</a></p>]]></description>
<content:encoded><![CDATA[<p>If you enjoy poker at all this game is fun, quick and addicting.</p><p><a href="http://sub-site.domain.com/review/name-review/">title review</a> has originally been posted on <a href="http://sub-site.domain.com">name sub site</a></p>]]></content:encoded>
<wfw:commentRss>http://sub-site.domain.com/review/name-review/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>

</item>


Jurre Hanema comments:

It turns out that you can not modify the output of Wordpresses built-in RSS widget, therefore it was needed to create a new widget with my modifications. I put the code here: http://pastie.org/2774024

If you insert this code through your functions.php, you should end up with a new widget called "RSS (WPQ)". It works just the same as the default widget, except it also outputs the category name & description next to every item. I did not make this behaviour configurable, as that would require the copying and modification of even more code.

I don't know if this produces the exact output you want, but further modifications should be pretty easy. If you search for my comment "Modify final output here" you may even be able to figure it out for yourself.


senlin comments:

Geweldig Jurre! Enorm bedankt! Werkt prima en makkelijk zelf aan te passen (heb de cat naam weggehaald)

translation:
Fantastic Jurre! Thanks so much! Works great en easy to adapt (deleted the cat name)

2011-10-27

Alex Sancho answers:

You can use a hook to inject your custom data inside each post on feed, the action is "rss2_item"


add_action( 'rss2_item', 'my_custom_function' );


senlin comments:

Thanks for your input Alex. Jurre however nailed it!

2011-10-28

Luis Cordova answers:

just put it on a widget right or use samsarin php


senlin comments:

I am not sure whether you understand the question. The widget does not by default show the description of the custom taxonomy of the custom post type...