2

I'm using fetch_feed to display an rss feed on a theme i'm working on.

I think the documentation implies that you can combine multiple feeds, can anyone confirm this, ideally with an example.

Thanks in advance! George

1 Answer 1

4

You can technically pass multiple feed URLs to fetch_feed() as an array, and it'll grab them all at once. But the return is a total mess, and I couldn't personally figure out how to parse it.

If nobody else knows how to do this with fetch_feed(), I can offer a solution using the SimplePie class (which fetch_feed actually uses anyway). The SimplePie class has some helper methods for parsing the return, and they make things pretty easy.

Try something like:

// Contains the SimplePie class
require_once (ABSPATH . WPINC . '/class-feed.php');

// New class instance
$feed = new SimplePie();

// You feed URLs
$feed->set_feed_url(array('http://rss.cnn.com/rss/cnn_topstories.rss', 'http://cuteoverload.com/feed/'));

// Do it!
$feed->init();
$feed->handle_content_type();

// Loop the results
foreach($feed->get_items() as $item) {

    echo $item->get_title();
    echo '<hr/>';

}

Additional SimplePie methods include get_permalink() and get_description().

The only downside to this approach is that is SimplePie is ever phased out of WordPress in favor of another class, this'll break.

UPDATE

As @Rarst pointed out in the comments, you don't need to access SimplePie directly. You can use its methods on the object that fetch_feed() returns. So the answer is much simpler than I thought:

$feed = fetch_feed(array('http://rss.cnn.com/rss/cnn_topstories.rss', 'http://cuteoverload.com/feed/'));

// Loop the results
foreach($feed->get_items() as $item) {

    echo $item->get_title();
    echo '<hr/>';

}
2
  • 1
    fetch_feed() returns SimplePie object, simply use its methods with return. It is bad idea to use SimplePie in WP directly because that will skip proper setup of caching, filters for options and cache lifetime.
    – Rarst
    Jan 26, 2011 at 6:26
  • @Rarst Thanks--didn't know you could access the SimplePie methods from the returned object. I'm amending my answer.
    – MathSmath
    Jan 26, 2011 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.