WordPress.org

Ready to get started?Download WordPress

Forums

How to show an error message if fetch_feed does not work? (4 posts)

  1. WPChina
    Member
    Posted 4 years ago #

    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 :(

  2. WPChina
    Member
    Posted 4 years ago #

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

  3. WPChina
    Member
    Posted 4 years ago #

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

  4. Jason
    Member
    Posted 4 years ago #

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

    try var_dump($rss);

Topic Closed

This topic has been closed to new replies.

About this Topic