>How to Import and Display Feeds in WordPress 2.8 and Beyond


Importing and displaying feeds in your WordPress themes is a great way to share additional content with your readers. Some good examples include:

  • Displaying your latest tweets, flickrs, or delicious saves
  • Displaying content from your other sites in your sidebar or footer
  • Providing a public link feed to which your readers may contribute or subscribe

Basically, anything that is available in any sort of feed format — RSS, XML, Atom, etc. — may be easily imported and displayed on your WordPress-powered site.

Prior to version 2.8, WordPress used Magpie and Snoopy functionality to make it easy to parse and display feeds. As described in my previous article on importing and displaying feeds in WordPress, including a feed using the built-in Magpie class is straightforward:

<?php include_once(ABSPATH.WPINC.'/rss.php');
wp_rss('http://domain.tld/your-feed/', 7); ?>

This code example will import and display the feed of your choice in WordPress versions 2.7 and older. For versions 2.8 and 2.9 (and possibly beyond), WordPress has changed from Magpie and Snoopy to SimplePie and FeedCache for retrieval, parsing and automatic caching of feeds. This is a good thing, even though SimplePie ceased development shortly after it was adopted by WordPress. Hopefully someone will pick it up and run with it.

For now, we just want to use WordPress to display external feeds on our sites. To do this, WordPress gives us the new fetch_feed() function, which retrieves, parses, and caches any RSS feed, even those generated by your own installation of WordPress.

To use this new function, we begin with a compatibility check, just in case we’re running an older version of WordPress. If the function exists, we proceed by including the required file, feed.php. Then, once we have the required file, we specify the feed URI as the (only) parameter of the fetch_feed function:

<?php if(function_exists('fetch_feed')) {

include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed('http://digwp.com/feed/'); // specify the source feed

} ?>

At this point, all of the XML data from our source feed is returned as a standard SimplePie object that we capture as a variable called “$feed”. With the feed data cached and ready, we may use a variety of SimplePie methods to manipulate and display the data according to our needs.

The first thing that we may want to do is specify the number of items to display:

$limit = $feed->get_item_quantity(7); // specify number of items

Here we are showing seven items, but you can choose any number you want. Next, we want to create an array containing all of our feed items so that we can then loop through it and display stuff like the title, the date, the post contents, and so on. Here is how we create our array of seven items:

$items = $feed->get_items(0, $limit); // create an array of items

Note that the first feed item is numbered zero (“0”) and the last feed item is specified by the previously declared $limit variable, so we know exactly how many items to include in the array.

At this point, we are ready to loop through the array and display data from each of our feed items. Before we do, however, we want to ensure that the feed isn’t empty. Once this is verified, we proceed to loop through each item and display its title, permalink URL, and post date:

Not bad, but what about post content? In addition to the post title, date, and permalink, it’s useful to be able to display an actual snippet from each feed item. No problem, just add the following method to your feed loop:

get_description(); ?>

You can also display the title of the feed itself with this snippet:

get_title(); ?>

Of course, there are many more types of data that may be extracted and displayed from your feed items. For more information, check the references at the end of this article.

For now, allow me to wrap things up by putting everything together into a nice, plug-&-play, copy-&-paste code snippet that is perfect for importing and displaying feed items on your WordPress-powered site:

<?php if(function_exists('fetch_feed')) {

include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed('http://feeds.feedburner.com/jquery/'); // specify the source feed

$limit = $feed->get_item_quantity(7); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items

if ($limit == 0) echo '
The feed is either empty or unavailable.
else foreach ($items as $item) : ?>

get_description(), 0, 200); ?>

This is the code that I use on my new site, jQuery Mix. It displays the following information:

  • Seven items from the jQuery.com feed
  • Permalink, date, and title for each item
  • First 200 characters of each item’s post content

This code snippet seems to work great and should be easy to customize to fit your specific needs. Check the docs for more information!

source : http://digwp.com/2009/11/import-and-display-feeds-in-wordpress/


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: