How to make your WordPress a RSS reader

Posted on Oct 30, 2010

WordPress is my blogging tool. What I admire WordPress for is its simplicity and extensibility. It contains a lot of useful functions, yet the code is simple and easy to read. It is also very customizable with a little modification of the existing code. The more I know about WordPress, the more I sympathize with their motto, “Code is Poetry”.

As an example of how easy to extend the functionality of WordPress, I here show a way to make a WordPress page which regularly fetches the contents of the blogs you like. It just takes the following 3 steps.

1. Write a blank WordPress page with a name you like. (e.g., “RSS”)

2. In your theme folder, find “single.php” and make a copy of it with the same name as the page you just made. (e.g., “rss.php”) If you don’t find, just make an empty text file.

3. Open the copy (or the empty text file) with any text editor and add the highlighted code below. Since you probably want to keep this page’s look the same as your theme, you can copy just the part of the highlighted lines for your necessity.

<?php

// List the RSS addresses of the blogs you want to subscribe
$url = array(
	'http://www.xkcd.com/rss.xml',
	'http://www.phdcomics.com/gradfeed_justcomics.php'
);

// Limit the number of the articles per blog as 1
function twentyten_feed_options( $feeds ) {
	$feeds->set_item_limit(1);
}
add_action( 'wp_feed_options', 'twentyten_feed_options' );

// Get the contents of the blogs
$feeds = fetch_feed($url);

get_header(); ?>

		<div id="container">
			<div id="content" role="main">

<?php
	foreach($feeds->get_items() as $item):
		$feed = $item->get_feed();
?>
				<div <?php post_class(); ?>>
					<h1 class="entry-title"><a href="<?php echo $item->get_permalink(); ?>" target="_blank"><?php echo html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8');?></a></h1>
					<div class="entry-meta">
						<span class="entry-date"><?php echo $item->get_date('l, F j, Y'); ?></span>
						<span class="author vcard"><a href="<?php echo $feed->get_permalink(); ?>" target="_blank"><?php echo $feed->get_title(); ?></a></span>
					</div>

					<div class="entry-content">
						<?php echo $item->get_content(); ?>
						<div class="clear"></div>
					</div><!-- .entry-content -->
				</div><!-- #post-## -->
<?php endforeach; ?>

			</div><!-- #content -->
		</div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

That’s it. Now you can see all your favorite blogs at one place of your blog.