This might be really simple, but I’m pretty hopeless when it comes to rss and WP. Where should I put the if ( is_wp_error( $feed ) ) {
in a code like this:
<?php
$rss = fetch_feed('' . $instance["feed_address"] . '');
$maxitems = $rss->get_item_quantity($instance["feed_count"]);
$rss_items = $rss->get_items(0, $maxitems);
$title = substr($item['title'],0,55)." ... ";
?>
<ul>
<?php if ($maxitems == 0) echo ''; else foreach ( $rss_items as $item ) : ?>
<li>
<h2><a class="description" href="<?php echo $item->get_permalink(); ?>" target="_blank"><?php echo substr($item->get_title(), 0, 49) . ' ...'; ?> <span class="tip"><?php echo strip_tags(substr($item->get_description(), 0, 215)) . ' ...'; ?> <?php echo ''.$item->get_date('j F Y | g:i a'); ?></span></a></h2>
</li>
<?php endforeach; ?>
</ul>
Thanks in advance.
EDIT:
I have the above code in a custom php page, not within core files. Does that make a difference? Anyhow, I managed to do this:
<?php
$rss = fetch_feed('' . $instance["feed_address"] . '');
if ( !is_wp_error( $rss ) ) :
$maxitems = $rss->get_item_quantity($instance["feed_count"]);
$rss_items = $rss->get_items(0, $maxitems);
$title = substr($item['title'],0,55)." ... ";
endif;
?>
<ul>
<?php if ($maxitems == 0) echo ''; else foreach ( $rss_items as $item ) : ?>
<li>
<h2><a class="description" href="<?php echo $item->get_permalink(); ?>" title="<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>" target="_blank"><?php echo substr($item->get_title(), 0, 49) . ' ...'; ?> <span class="tip"><?php echo strip_tags(substr($item->get_description(), 0, 215)) . ' ...'; ?></span></a></h2>
</li>
<?php endforeach; ?>
</ul>
The point being if ( !is_wp_error( $rss ) ) :
, atleast it loads the feeds and everything seems to be working, but is this ok? The error may or may not appear from time to time, so testing is a bit hard. But before, when the error stroke, the feed stopped the loading that instant and did not load the other feed (I have two) after that either. With the code I posted above, will it skip the feed receiving the error and load the other one?
Thanks.
EDIT:
Thank you very much, without the code above, the error occurs as expected, but with it, the feed is just left empty and the next one is loaded like usual. You rock!
Follow
$rss = fetch_feed('' . $instance["feed_address"] . '');
and find the function inwp-includes/feed.php
:As you can see,
fetch_feed()
may return a WP_Error object. So check it just below the call to this function: