Post Pic

How to display your latest tweets on your WordPress blog without any plugin

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.

<?php
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);
?>

<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 $item->get_title(); ?>
</a>
</li>
<?php endforeach; ?>
</ul>

Thanks to Smashing Magazine for this great code!

17 Responses

Feb 08 2012 12:16

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

Feb 10 2012 18:56

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:

Feb 10 2012 18:57

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

Feb 11 2012 19:46

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

Feb 13 2012 00:51

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?

Feb 14 2012 00:31

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 :
https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=catswhocode&count=3

Try with this URL :
http://search.twitter.com/search.rss?q=from%3Acatswhocode&rpp=3

Or with this plugin that handles connection attempts on the two URLs :
http://wordpress.org/extend/plugins/juiz-last-tweet-widget/
(the cache system will be improved in the week thanks to the tip of @Mike Rogers)

Thanks.

Mar 01 2012 00:57

@Geoffrey,

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

Henry

Apr 21 2012 05:09

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

May 10 2012 08:06

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.

Jun 06 2012 19:21

Thanks for this awesome code :)

Jul 10 2012 15:30

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

Jul 24 2012 10:43

@Geoffrey

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

Jan 10 2013 01:27

Worked perfect. Thanks!

Feb 17 2013 21:41

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

Feb 18 2013 10:00

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.

Feb 22 2013 04:30

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

Apr 23 2013 20: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

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox