Setting up an RSS feed for your blog can be a little tricky. I don’t claim to be an RSS feed expert, but here’s how I did it at Bob’s Guides.
The Resource
Create a new Resource and fill in the Title, Summary (introtext) and Alias fields.
I used the Title “What’s New at Bob’s Blog”, and for the Summary (introtext) field:
“Bob’s Guides Blog RSS Feed”. Important: Using the Template drop-down list at
the upper right, select the (empty)
option for no Template.
I used getResources to create the feed, though pdoResources might also work and would be faster. Here’s the code for my Resource 309. It goes in the Resource Content field:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>[[*pagetitle]]</title>
<link>
[[~[[*id]]? &scheme=`full`]]</link>
<description>[[*introtext:cdata]]</description>
<language>[[++cultureKey]]</language>
<ttl>120</ttl>
<atom:link href="[[~[[*id]]? &scheme=`full`]]" rel="self" type="application/rss+xml"/>
[[!getResources?
&tpl=`rssItem`
&resources=`-183,-184,-198`
&limit=`10`
&includeContent=`0`
&includeTVs=`1`
&processTVs=`1`
&showHidden=`1`
&hideContainers=`1`
&sortby=`editedon`
]]
</channel>
</rss>
The &limit
property says how many articles to show. I have it set to
10, but you can use whatever number you like. You can set it to 0
if you want to show them all, though I think it’s generally a bad idea.
I chose editedon
for the &sortby
property because I wanted
pages that were recently updated to bubble to the top of the list. You may choose
to use createdon
or publishedon
. If you select
publishedon
, unpublishing and re-publishing an article will move
it to the top of the list. You can use any Resource field here, though the ones
listed above are the most common, and it makes sense to use a date field, so you’ll
be showing the most recent articles.
The default sort direction is descending, so the tag above will show the 10 most recent articles. If you need more complex searching and/or sorting, see the getResources documentation. It’s possible to sort by a TV value as well.
The &resources=`-183,-184,-198`
property is optional. It lists pages I
don’t want to show in the feed.
The &parents
property is set to 282
, which is the ID of my
main Blog container page. Change that to the ID of yours, or if your blog has
separate sections, you can use a comma-separated list of IDs here.
Be sure to make the resource published, and you may want it hidden from menus. Don’t forget to save it.
The RSS Item Tpl
The format of an individual item in the feed is in the Chunk specified in the
&tpl
property. My Tpl Chunk is called rssItem
,
but you can call it whatever you like. Here is the code of
my rssItem
Tpl Chunk:
<item>
<title>[[+pagetitle:htmlent]]</title>
<link>
[[++site_url]][[~[[+id]]]]</link>
<description>
[[+introtext:default=`[[+content:ellipsis=`600`]]`:cdata]]
</description>
<pubDate>[[+publishedon:strtotime:date=`%a, %d %b %Y %H:%M:%S %Z`]]</pubDate>
<guid isPermaLink="true">[[++site_url]][[~[[+id]]]]</guid>
<dc:creator>
Bob Ray
</dc:creator>
</item>
Be sure the name of the Tpl Chunk you create matches the Chunk specified in the
&tpl
property of your getResources Snippet tag.
If your site is multi-lingual, you might suspect that the :date
output
modifier should be replaced with the :strftime
modifier because the
PHP function strftime()
will respect the locale
setting
and date()
won’t. This isn’t necessary, though, because the MODX
:date
modifier actually calls the strftime()
function
rather than the date()
function.
&parents
property selecting the parents of the pages you want
included, and the &resources
property specifying with minus signs,
the pages you want to exclude.
The RSS Icon Image Tag
First, find an RSS icon you like somewhere on the Web and download it to somewhere
in your assets/
directory. I put mine in the
assets/images/
directory, but it can go wherever you like.
If you put it somewhere else, adjust the URL in the next code section to
point to it.
This HTML code goes wherever you want the RSS icon to appear. I put it near at the bottom of the Template for the page that displays the list of posts, and at the bottom of the Template for the page that shows an individual post.
<a href="[[~309]]">
<img style="border:none;"
src="[[++site_url]]assets/images/feed-icon-28x28.png" height="28" width="28"
alt="RSS Feed" title="RSS Feed"/></a>
Notice that the href
tag above is for Resource 309. That’s the ID of
the Resource I created. Yours will have a different ID. Once you create and save
the Resource below, make a note of its ID (in parentheses next to the name in the
Resource Tree) and change the 309 in the code of your Templates to that new ID.
Testing Your RSS Feed
After clearing the site cache, you should be able to see the XML code of your RSS feed by clicking on the RSS icon on one of your blog pages. If not, review the steps above to make sure you haven’t missed something. In order to make sure it works in all RSS readers and syndication services, it’s also a good idea to vadidate the feed using a tool such as the W3C’s Feed Validator. XML and the RSS specifications are strict and errors may cause undesired results or your feed not working properly.
Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.