A list of category feeds on a page in WordPress

A blog implementation project I’m working on at the moment is using the categorisation capability of WordPress to support SDI for our users. By using categories our users can either subscribe to the RSS feeds for each category, or subscribe to a mailing list powered by the excellent Subscribe2 plugin and select specific categories.

In this way our users are in control of the information they receive and can filter out, at a broad level, those posts that are of no interest to them.

We have our categories listed in the sidebar on our site. The wp_list_categories template tag has the ability to output a link to the RSS feed for a category but the output didn’t fit in our layout. With this in mind I needed to come up with a different way of displaying the information.

Rather than create a plugin I decided to use the a custom page template instead. In this way I could construct a page that I could edit the text of the page using the standard tools, and I could also create a list of RSS feeds dynamically, without the need to go through creating a plugin.

I created a custom page template, basically a copy of the standard page template, and then added the following code into the appropriate place.


< ?php
// Get the list of categories
$categories = get_categories('type=post&hide_empty=0&orderby=id');
$site_url = get_option('siteurl');

// Output the list
print '<ul>';

foreach($categories as $category) {
  print '<li><a href="' . $site_url . '/category/' . $category->category_nicename . '/feed/' . '" ';
  print 'title="Direct link to the RSS feed">';
  print $category->name . '</a>';
  print '<a href="' . $site_url . '/category/' . $category->category_nicename . '/feed/' . '" ';
  print 'title="Direct link to the RSS feed" style="text-decoration: none">';
  print ' <img src="';
  bloginfo('template_url');
  print '/images/feed-icon-14x14.png" height="14" width="14" alt="" border="0"/></a></li>' . "\n";
}

print '';
?>

Users can select from the generated list, the URLs to the RSS feeds that are of interest to them.

Posted in Musings, PHP, Programming. Tags: , , .

Leave a Reply