Complete and flexible blogging platform for ProcessWire

Render RSS

We use the method renderRSS() to return an RSS feed of given pages.

<?php
 /**
 * Render an RSS feed of the given pages.
 *
 * Note that you should not output anything further after calling this, as it outputs the RSS directly.
 *
 * @access public
 * @param PageArray $items Pages to include in the feed
 * @param string $title Title of the feed. If not provided, pulled from current page.
 * @param string $description Description of the feed. If not provided, pulled from current page.
 *
 */
 public function renderRSS(PageArray $items, $title = '', $description = '')

The method accepts three arguments, $items, $title and $description.

The first argument $items needs to be a PageArray. and is required. These will be the items to include in the RSS feed. Internally, the method uses the module RSS Feed Generator to render the RSS feed. This needs to be installed in order for this method to work. Otherwise, you will get an error that such a module was not found. 

The second argument $title is not required and if left empty defaults to the page's 'headline' field (field name must match this) and if such does not exist, the page's title. If specified, the value has to be a string

The third argument $description is not needed. If specified, it must be a string. If left empty, this will be pulled from a field in the page called 'summary'. If that doesn't exist, a description will be sought from a field on the page called 'meta_description'. If such does not exist either, no description will be returned.

Note, for the RSS to work, you should not output anything further after calling this method, as it outputs the RSS directly. If not, you will get an error. Below are some example usages.

In this example, we pass the current page's children as $items, the home page's 'headline' field or title as $title and its 'summary' field as $description to renderRSS(). Note our use of a URL segment '/rss/' in this particular case.

<?php
//first we call MarkupBlog
$blog = $modules->get('MarkupBlog');

//we want to view the rss of posts if URL segment /rss/ is specified
if($input->urlSegment1) {
    //rss feed
    if($input->urlSegment1 != 'rss') throw new Wire404Exception();
    $homepage = $pages->get('/'); 

    //render rss
    $blog->renderRSS($page->children("limit=10"), $homepage->get('headline|title'), $homepage->get('summary')); 
    
    //this is important: stops output of any other markup except the RSS xml
    return;
}

The above code will output an RSS feed similar to the following.

<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title>Home</title>
<link>http://localhost/pwtests/blog/posts/</link>
<description/>
<pubDate>Wed, 27 Aug 2014 18:57:13 +0100</pubDate>
<ttl>60</ttl>
<item>
<title>British Weather</title>
<description>
<![CDATA[ ]]>
</description>
<link>
http://localhost/pwtests/blog/posts/british-weather/
</link>
<guid>
http://localhost/pwtests/blog/posts/british-weather/
</guid>
</item>
</channel>
</rss>

In the following example, we want to output an RSS feed of a limited number of Posts that are marked with a particular Tag. We place the code in the Template File used by our individual Blog Tag pages. Again, we use a URL segment to indicate we want to render an RSS feed. In this case, we choose only to specify the argument $items.

<?php
//first we call MarkupBlog
$blog = $modules->get('MarkupBlog');

//we want to view the rss of posts using this tag
if($input->urlSegment1) {
   
	// rss feed
	if($input->urlSegment1 != 'rss') throw new Wire404Exception();
	
	$posts = $pages->find("blog_tags=$page, limit=10");//grab some posts using this tag
	
	$blog->renderRSS($posts); 
	
	//this is important: stops output of any other markup except the RSS xml
	return;
}

Summary

In this lesson we have looked at how to render an RSS feed of our Blog pages using the method renderRSS($items, $title = '', $description = ''). It is important not to output anything else after this method has been called since it outputs the RSS directly.