Support » Fixing WordPress » How to show an error message if fetch_feed does not work?

  • WPChina

    (@wordpresschina)


    I am using this code to display an RSS feed. However sometimes the website with the RSS feed is unavailable or does not return a RSS feed. In that situation, my blog freezes when it displays at that point because of an error and the page is not fully loaded:

    <?php
    include_once(ABSPATH . WPINC . '/feed.php');
    $rss = fetch_feed('http://FEED-URL-GOES-HERE');
    $rss_items = $rss->get_items( 0, $rss->get_item_quantity(5) );
    if ( !$rss_items ) {
        echo 'no items';
    } else {
        foreach ( $rss_items as $item ) {
            echo '<p><a href="' . $item->get_permalink() . '">' . $item->get_title() . '</a></p>';
        }
    }
    ?>

    Is there a way to show an error message if the feed does not show? I am using “no items” in there, but that seems to only show if the feed is available but there are no items. It does not show if maybe my blog can not access that Rss feed for a few hours 🙁

Viewing 3 replies - 1 through 3 (of 3 total)
  • WPChina

    (@wordpresschina)

    Sorry, anybody have any thoughts on this on how Simplepie handles this?

    WPChina

    (@wordpresschina)

    Sorry, anybody have ideas about this? This is a new tag placed in WordPress recently and documentation is sparse…..

    I *think* fetch_feed will return the error message in the array if there is one.

    try var_dump($rss);

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to show an error message if fetch_feed does not work?’ is closed to new replies.