Textpattern tips, tutorials and code snippets

RSS feeds for tags

In the same vein as Create an RSS Feed for last comments and inspired by a forum question, here is how to create a feed filtered on a given tag.

This is made possible thanks to (once again) the plugin rah_external_output:

  1. Install and activate rah_external_output
  2. Create a new form and name it rah_eo_tagfeed.
  3. Copy-paste, fill in the blanks and tune according to your needs

The RSS feed code

; Content-type: text/xml
<?xml version="1.0" encoding="utf-8"?>

<txp:php>global $variable; $variable['tag'] = gps('tag');</txp:php>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>

 <generator>http://textpattern.com/</generator>
 <title><txp:site_name /></title>
 <description> ... </description>
 <link><txp:site_url /></link>
 <atom:link href="<txp:site_url />?rah_external_output=tagfeed&amp;tag=<txp:variable name="tag" />" rel="self" type="application/rss+xml" />
 <pubDate><txp:php>echo safe_strftime('rfc822');</txp:php></pubDate>

 <txp:article_custom limit="100" break="">

   <txp:if_keywords keywords='<txp:variable name="tag" />'>
     <txp:variable name="item_content" value='<![CDATA[<txp:body />]]>' />

     <item>
       <title><txp:title no_widow="0" /></title>
       <link><txp:permlink /></link>
       <guid><txp:permlink /></guid>
       <pubDate><txp:posted format="rfc822" /></pubDate>
       <description><txp:variable name="item_content" /></description>
       <content:encoded><txp:variable name="item_content" /></content:encoded>
       <dc:creator><txp:author /></dc:creator>
     </item>

   </txp:if_keywords>
 </txp:article_custom>

</channel>
</rss>

Feed’s URL

The raw URL is http://www.example.com/?rah_external_output=tagfeed&tag=thetag

  • tagfeed as in rah_eo_tagfeed
  • tag is used inside the txp:php code snippet to retrieve the value to filter on
    * thetag is this value

To have a URL of this form http://www.example.com/tag/thetag/feed, add this rule to your .htaccess:

RewriteRule ^tag\/(.*)\/feed$ /?rah_external_output=tagfeed&tag=$1 [R=301,L]

Some elements you can tune

In this example, tags are stored as keywords. If you want to use a custom field, replace the <txp:if_keywords> ... </txp:if_keywords> pair with <txp:if_custom_field name="Your custom field name" value='<txp:variable name="tag" />'> ... </txp:if_custom_field>

If you set the rewriting rule in .htaccess, atom link becomes <atom:link href="<txp:site_url />tag/<txp:variable name="tag" />/feed" rel="self" type="application/rss+xml" />

Feed description (<description> ... </description>): replace the ellipsis with, for example <txp:site_slogan /> - <txp:variable name="tag" />

Item content: replace or augment <txp:body /> with your own content (excerpt, article_image, etc)

The limit for article_custom is intentionally high to avoid empty feeds but can conversely produce huge lists. This can be enhanced by counting “on the fly”, but is beyond the scope of this tip.