topher

SimplePie is some sweet php to help you render RSS feeds on web pages. It can do so many different things that it can be a bit hard to figure out how to make it do the thing YOU want it to do.

I wanted it to render an unordered list of headlines, so I wrote this function:

// get rss and convert it to an unordered list
function feed2ul($feed_url = "", $end = "5") {

    // Initialize new feed
    $feed = new SimplePie();
    $feed->set_feed_url($feed_url);
    $feed->init();

    $num = 0;

    $output .= '<ul class="feed">' . "n";

    // Loop through all of the items in the feed
    foreach ($feed->get_items() as $item) {

        if($num < $end) {
            $output .= '<li><a href="' . $item->get_link() . '">' . $item->get_title() . '</a></li>' . "n";
        }
        $num++;
    }

    $output .= '</ul>' . "n";

    return $output;

// end feed2ul
}

You just feed it a url, and optionally how many feed items you want, like this:

<?php print feed2ul('http://www.sportsspectrum.com/daily/daily.rss', '5'); ?>

One thought on “RSS to UL with SimplePie

Leave a Reply

Your email address will not be published. Required fields are marked *