DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

If you’re using Twitter, you maybe want to display your latest tweets on your blog. You can use a plugin for that, or you can use this code to display your tweets without using any plugin.

Simply paste the following code anywhere in your theme files, where you want the tweets to be displayed.
Don’t forget to update the code with your username on line 3. Max items to display can be defined on line 4.

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

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

Thanks to Smashing Magazine for this great code!



  1. Fatal error: Call to undefined method WP_Error::get_item_quantity() in [FILE]

  2. The fetch_feed() function will by default cache the feed for 12 hours. You can change this to every hour by adding the following code to your function.php file:

  3. function filter_handler( $seconds )
    // change the default feed cache recreation period to 2 hours
    return 7200;

    add_filter( ‘wp_feed_cache_transient_lifetime’ , ‘filter_handler’ );

    From: http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_feed_cache_transient_lifetime

  4. Thanks for a useful and simple solution. I have one question. Number of tweets is defined here? $ rss-> get_item_quantity (3)

  5. I read on the Twitter development blog that “Twitter API only allows clients to make a limited number of calls in a given hour”. If my site gets quite a lot of hits per hour, does that mean the latest tweet won’t be displayed?

  6. Hi,

    Be carefull, the Twitter API has a limited number of call per hour. You can see it with the “Live HTTP headers” plugin of Firefox.
    “X-RateLimit-Remaining: 147”
    You can make 150 calls per hour from the same IP server.
    This limit is exceeded quickly with a shared server.

    You can also limit the number of tweets directly to the query :

    Try with this URL :

    Or with this plugin that handles connection attempts on the two URLs :
    (the cache system will be improved in the week thanks to the tip of @Mike Rogers)


  7. @Geoffrey,

    Thanks for replying. I will give the plugin a shot.


  8. Thanks Jean-Baptiste! Any idea how to add the Twitter feed on a regular html, non-Wordpress page?

  9. Hi, I’m a novice with WordPress, is there an advantage to not using a plugin, vs. using one? Seems like a plugin would be easier to uninstall at some future point, if needed.

  10. Thanks for this awesome code 🙂

  11. how do you show the thumbnail image or the twitter user?

  12. @Geoffrey

    The juiz-last-tweet-widget plugin is great.

  13. Worked perfect. Thanks!

  14. Very nice code thanks, worked very well, and we can customise it a lot!!!!

  15. Unfortunately, this is not working on WP 3.5.1. I get the same error as Dimitar:

    Fatal error: Call to undefined method WP_Error::get_item_quantity() in [FILE]

    Any fix is appreciated.

    Thank you.

  16. Just used it in WP 3.5.1 and it works fine. If you want you can use my RSS plugin for this.

  17. How would I go about stopping the html tags being stripped from the title or description? Would love to use this but need to keep links in the feed

Leave a Comment

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