WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/fetch rss

Description

Retrieves an RSS feed and parses it. Uses the MagpieRSS and RSSCache functions for parsing and automatic caching and the Snoopy HTTP client for the actual retrieval.

Usage

 <?php
include_once(ABSPATH WPINC '/rss.php');
$rss fetch_rss($uri);
?> 

Example

To get and display a list of links for an existing RSS feed, limiting the selection to the most recent 5 items:


<h2><?php _e('Headlines from AP News'); ?></h2>
<?php // Get RSS Feed(s)
include_once(ABSPATH WPINC '/rss.php');
$rss fetch_rss('http://example.com/rss/feed/goes/here');
$maxitems 5;
$items array_slice($rss->items0$maxitems);
?>

<ul>
<?php if (empty($items)) echo '<li>No items</li>';
else
foreach ( 
$items as $item ) : ?>
<li><a href='<?php echo $item['link']; ?>
title='<?php echo $item['title']; ?>'>
<?php echo $item['title']; ?>
</a></li>
<?php endforeach; ?>
</ul>

Ideally this kind of work (limiting the number of returns) would be done with wp_rss() instead of fetch_rss, since wp_rss already has that capability built in. However, wp_rss bizarrely inserts a br tag after each item, even though it's already in an li container. Therefore, if you need to actually control the output, you'll need to use fetch_rss as above.

Parameters

$uri
(URI) (required) The URI of the RSS feed you want to retrieve. The resulting parsed feed is returned, with the more interesting and useful bits in the items array.
Default: Nonerray.

Related

wp_rss