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 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 Articles Container Template and the Article Template.
[code language=”html”]
<br/><a href="[[~309]]"> <img style="border:none;"
src="[[++site_url]]assets/images/feed-icon-28×28.png" height="28" width="28"
alt="RSS Feed" title="RSS Feed"/></a>
[/code]
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.
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. Here’s the code for my Resource 309. It goes in the Resource Content field:
[code language=”html”]
<?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`
&parents=`282`
&resources=`-183,-184,-198`
&limit=`10`
&includeContent=`0`
&includeTVs=`1`
&processTVs=`1`
&showHidden=`1`
&hideContainers=`1`
&sortby=`editedon`
]]
</channel>
</rss>
[/code]
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 updated pages 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 probably 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:
[code language=”html”]
<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>
[/code]
Be sure the name of the Tpl chunk you create matches the chunk specified in the &tpl
property of your getResources Snippet tag. Be careful, the names are case-sensitive.
If your site is multi-lingual and you’ll have RSS feeds in different languages, you’ll probably want to use the strftime
output modifier in your Tpl chunk rather than the date
modifier — strftime
will respect the locale
setting and date
won’t.
Testing
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.
For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.
Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!