x
 
Create New Page
 View Previous Changes    ( Last updated by alienne )

RSS Comment Feed for Each Entry

To create a per-entry RSS feed that will show all comments, follow the following steps. (So far I’ve only set this up in RSS 2.0; Atom will probably follow soon, since I like it better anyway. But i wanted to get this out, to help someone who asked for it.)

Part the first: setting up your feed

First, create a template of type RSS. Name it whatever you want; I named mine ‘rsscomments’. Since it’s of necessity for a single weblog, I’d recommend putting it in the Template Group for that weblog, but of course you may put it wherever you want.

Set it up as follows:

{exp:rss:feed weblog="YOUR_WEBLOG_NAME"}

<?xml version
="1.0" encoding="{encoding}"?>
<rss version="2.0"
    
xmlns:dc="http://purl.org/dc/elements/1.1/"
    
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    
xmlns:admin="http://webns.net/mvcb/"
    
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    
xmlns:content="http://purl.org/rss/1.0/modules/content/">

This stuff just starts your feed and defines your format: RSS 2.0. It should detect {encoding} from your global settings. (Multilingual sites are beyond the scope of this article, but you can probably sort it out.) Leave all this stuff alone, except where it says YOUR_WEBLOG_NAME. Replace it with the name of a SINGLE WEBLOG.

<channel>
    
{exp:weblog:entries}
    
<title>{exp:xml_encode}{title}{/exp:xml_encode}</title>
    <
link>{title_permalink=ENTRY_PERMALINK_HERE}</link>
    <
description>{exp:xml_encode}{summary}{/exp:xml_encode}</description>
    <
dc:language>en-us</dc:language>
    <
dc:creator>{email}</dc:creator>
    <
dc:rights>Copyright {gmt_date format="%Y"}</dc:rights>
    <
dc:date>{gmt_entry_date format="%Y-%m-%d T;%H:%i:%s%Q"}</dc:date>
    <
admin:generatorAgent rdf:resource="http://www.pmachine.com/" />
{/exp:weblog:entries}

This is the piece that’s the description of your feed, overall, not a specific RSS entry—so in our case, it contains information about our weblog entry. Here I’m assuming that you’re using the “standard” Entry Fields, so the <description> element is the {summary} of your entry, but it really doesn’t matter. It SHOULD be under 500 characters, whatever it is, but you can set it to whatever you want. (Use your main entry field with {exp:word_limit}, for instance.)

Note the SPACE between the ’%d’ and the ‘T’ in the <dc:date> element ; REMOVE THAT SPACE. (It’s a limitation of the Wiki software, it munges the characters when i butt them together.)

{exp:comment:entries orderby="date" sort="desc"}
    
<item>
      <
title>{exp:xml_encode}Comment by {name}{/exp:xml_encode}</title>
      <
link>{exp:xml_encode}{url_or_email_as_link}{/exp:xml_encode}</link>
      <
description>{exp:xml_encode}{comment}{/exp:xml_encode}</description>
      <
content:encoded><![CDATA[{comment}]]></content:encoded>
    <
dc:date>{gmt_entry_date format="%Y-%m-%d T;%H:%i:%s%Q"}</dc:date>
    </
item>
{/exp:comment:entries}

This part loops through your comment entries. Make sure to order them by date and sort=“desc”—i found that even though those are the theoretical defaults, it wasn’t working right for me.  Everything else in here should be pretty self-explanatory, and you shouldn’t need to change any fields. (I’m not exactly sure you need both <description> and <content:encoded>, but in my testing it doesn’t seem to HURT anything, so i just left it.

As above: Note the SPACE between the ’%d’ and the ‘T’ in the <dc:date> element ; REMOVE THAT SPACE. (It’s a limitation of the Wiki software, it munges the characters when i butt them together.)

</channel>
</
rss>

{/exp:rss:feed}

And this closes out your <channel> and your feed! Voilà, you’re done with the template!

part the second: adding your feed to your entries

This part’s easy. Just add the following wherever you want the feed link to appear (edit as you see fit). Note that this must be on a single-entry page, although it doesn’t have to be an actual comments page.

<a href="{title_permalink=TEMPLATE_GROUP/rsscomments}">RSS comments feed for this entry</a>

Category:RSS Category:Comments Category:Templates

Categories: