CREATING AN RSS FEED FOR A MULTI-RECORD SECTION - Feb 17th, 2015


If you’ve had mixed results trying to create an RSS feed for a multi-record section you’re not alone. There seem to
be many “details” for the “Devil” to hide in.

Here’s an approach that worked for me which you can build on for your own uses.

For a table called “current_events”, I wanted to show the most current records in my events list and limit the RSS
display to only the 2 most recently created records. I also wanted a masthead logo and a link to the main event’s page
to appear at the top of the feed.

The code that I finally came up with for the XML file I named happening-now-rss.xml.php is:



<?php header('Content-type: application/xml; charset=utf-8'); ?><?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>

<?php

require_once "/your_server_path/cmsAdmin/lib/viewer_functions.php";

list(
$current_eventsRecords, $current_eventsMetaData) = getRecords(array(
'Table name' => 'current_events',
'orderBy' => 'createdDate DESC',
'limit' => '2',
));

?>

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="http://www.your_domain.org/rss/happening-now-rss.xml.php" rel="self" type="application/rss+xml" />
<title>AHAPPENING NOW RSS FEED</title>
<link>http://www.your_domain.org/events.php</link>
<description>Arts Events Worth Knowing About</description>


<language>en-us</language>

<item>
<title>www.your_domain.org</title>
<description>
<![CDATA[
<div align="left">
<a href="http://www.artistsofpalmbeachcounty.org"><img src="http://www.your_domail.org/images/LOGO.png" /></a><br
/>These are the latest two events that were added to our <b>Happening Now</b> events list. For a complete list of events
<a href="http://www.your_domain.org/events.php" target="_blank">click here</a>.<br /><b><i>The date is when the event
was added.</i></b></div>
]]>
</description>
<guid isPermaLink="true">http://www.your_domain.org</guid>
<pubDate><?php echo date('r'); ?></pubDate>
</item>

<?php foreach ($current_eventsRecords as $record): ?>
<item>
<?php $title = htmlspecialchars($record['title']); ?>
<title><![CDATA[<?php echo strtoupper($title); ?><?php if ($record['end_date']): ?> - Now through <?php echo
$record['end_date'] ?><?php endif; ?> ]]></title>
<link>http://www.artistsofpalmbeachcounty.org<?php echo $record['_link'] ?></link>
<description><?php echo htmlspecialchars($record['rss_description']); ?></description>
<pubDate><?php echo date("D, d M Y H:i:s O", strtotime($record['createdDate'])) ?></pubDate>

<guid isPermaLink="true">http://www.your_domain.org<?php echo $record['_link'] ?></guid>
</item>
<?php endforeach ?>

</channel>
</rss>



Here are some of the things that I learned along the way.

This code in the example above makes Atom feeds happier.:



<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="http://www.your_domain.org/rss/happening-now-rss.xml.php" rel="self" type="application/rss+xml" />



You can include an image in your channel (not in an item) but it may or may not appear at the upper right corner of your
feed depending on the feed reader that you’re using.

If you’re embedding an image in the <channel> area it’s preferable to have your image title match the channel
title.

If you want to include a logo image, you can try to include it in a separate <item> instead if in the channel area (it
will not always appear at the top of your feed unless you add <pubDate><?php echo date('r'); ?></pubDate> to that item).

The date format "D, d M Y H:i:s O insures that your date will render in the appropriate format for RSS feeds.



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service