2

I've implemented a way to show the Twitter feed without a plugin. It's worked for weeks(months even, on a different site), but is now giving me trouble. Here is the error:

Fatal error: Call to undefined method WP_Error::get_item_quantity() in ... home.php on line 27

Line 27: $maxitems = $rss->get_item_quantity(3);

<?php
include_once(ABSPATH . WPINC . '/feed.php');
$rss = fetch_feed('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=urbanpalatela');
$maxitems = $rss->get_item_quantity(3);
$rss_items = $rss->get_items(0, $maxitems);
?>

<ul>
<?php if ($maxitems == 0) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<li>
<a href='<?php echo $item->get_permalink(); ?>'>
<?php echo str_replace('UrbanPalateLA: ', '&bull;', $item->get_title()); ?>
</a>
</li>
<?php endforeach; ?>
</ul>

Any ideas what could be causing the error?

2 Answers 2

1

I would say you are probably receiving a WP_Error on first run.

around the $maxitems and $rss_items lines, add:

if( ! is_wp_error( $rss ) ) {
    $maxitems = $rss->get_item_quantity(3);
    $rss_items = $rss->get_items(0, $maxitems);
}

As seen in the example.

4
  • Thanks for your answer. The error is now gone, but the feed simply says "No items." instead of showing the tweets. Any idea what could be causing this? Oct 10, 2012 at 17:49
  • I'm not sure, I haven't used this before. In the WordPress codex, they have this line above the fetch_feed(): ` include_once(ABSPATH . WPINC . '/feed.php');`. Perhaps try including the feed.php file, it may include some things required for this to work. Oct 10, 2012 at 17:56
  • Hm, I do have that in the second line. Oct 10, 2012 at 18:03
  • As mentioned on a below comment, I tried adding the link you are using to Google Reader and it gave me an error. Perhaps you mispelled the username.. >_> Oct 10, 2012 at 20:03
0

The twitter url does not seem to be valid. You can prevent this error twofold because it relies on a 3rd party.

First check if the feed is empty before you use $maxitems , not after.

For example:

include_once(ABSPATH . WPINC . '/feed.php');
$rss = fetch_feed('https://api.twitter.com/1/statuses/....');

if ($rss->get_item_quanity != 0) { 
   $maxitems = $rss->get_item_quantity(3);
   //rest of your code..
} else { 
   echo 'No feed available'; }

Edit: Eric's example is probably better because it will be handled by WP using if (!is_wp_error( $rss ) )

Second you twitter url is not returning anything, thus the error, this could be because the API has changed, you will have to refer to the twitter docs, https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline

2
  • I don't think it's necessarily anything to do with Twitter- I'm using this exact bit of code on a different website without any issues. The only difference is the username being used. Oct 10, 2012 at 17:50
  • You can see that the direct link you're using, api.twitter.com/1/statuses/… does not return anything.
    – Wyck
    Oct 10, 2012 at 18:46

Your Answer

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

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