Import And Display RSS Feeds In WordPress Using Fetch_Feed()

Today’s featured WordPress tutorial is a hack made by Dan Gayle, and it enables you to display any RSS feed on your blog. It may have been around for quite a while but it is still as useful now as it was back then.

There are a whole lot of tutorials out there that teach you how to pull up feeds into WordPress but as Dan pointed out in his article, which I’ll indicate later at the end of this post, they’re pulling the feed on the server side and are [likely] using the deprecated WordPress Codex ( wp_rss() function).

His method retrieves feed via mini-feed-loop. It uses WP’s SimplePie’s fetch_feed() functionality so you have more control over how you display your feed.

<?php
include_once(ABSPATH . WPINC . '/rss.php');
$feed = 'http://example.com/feed/';
$rss = fetch_feed($feed);
if (!is_wp_error( $rss ) ) :
$maxitems = $rss->get_item_quantity(3);
$rss_items = $rss->get_items(0, $maxitems);
if ($rss_items):
echo "<ul>\n";
foreach ( $rss_items as $item ) :
echo '<li>';
echo '<a href="' . $item->get_permalink() . '">' . $item->get_title() . "</a>\n";
echo '<p>' . $item->get_description() . "</li>\n";
endforeach;
echo "</ul>\n";
endif;
endif;
 ?>

You can pretty much paste the code wherever you like to display the feed, just don’t forget to change the URL feed (http://example.cpm/feed/) with that of your own. You can also change the number of items to be displayed by changing the numeric value on line 6 (get_item_quantity(3);).

Incoming search terms for the article:

Related Posts

Creating a Psychedelic Art Effect in Your Portraits

EWWW Image Optimizer

Inspirational Photo Retouches By Cristian Girotto

Create Your Own Sticker Design Via Photoshop