RSS is often used to publish frequently updated works, such as blog entries, news headlines etc. in a standardized format. An RSS document includes full or summarized text, plus metadata such as publishing dates and authorship. A standardized XML file format allows the information to be published once and viewed by many different programs. They benefit readers who want to subscribe to timely updates from favorite websites or to aggregate feeds from many sites into one place.

[adsense-banner]

WordPress has built-in functionality for parsing RSS files from other sources, this means that you can fetch the latest blog posts or articles from another site and display them on your own site. The RSS parser shipped with WordPress let’s you define an RSS feed to read from and gives you an array where each entry describes a single post or article.

include_once(ABSPATH.WPINC.'/feed.php');
$parser = fetch_feed('http://www.webdevcorner.net/feed');

This code includes the file we need for the feed parser, fetches the feed from the specified url, make sure to specify a valid RSS feed, the resulting feed is returned as a standard SimplePie object. Now lets do something with this object.

if (!is_wp_error( $parser ) ) :
    // Fetch only the 5 last items from this feed
    $maxitems = $parser->get_item_quantity(5); 

    // Create an array holding the items
    $items = $parser->get_items(0, $maxitems); 
endif;

Here you can see we fetch the 5 last items from our RSS feed, note that we put this inside a if statement that checks if our parser object has an error. Then we create an array ($items) to hold our items.

    The feed is empty.'; else // Loop through each feed item and display each item as a hyperlink. foreach ( $items as $item ) : ?>;  
  •         get_title() ); ?>
  • ;
;

Here we looped through our items and output a simple list of links with the titles from our RSS feed. You don’t have to worry about this code pulling the RSS feed on every request, because by by default the function fetch_feed caches the results for 12 hours. This may be way too long for some people. But don’t worry, on the next page you will learn how to change this interval so hang tight.