Display Latest Tweets on Your WordPress Blog Without Using Any Plugin

If you have twitter account and want to display latest tweets on your blog, some people will using plugin to display the latest tweets, but you also can use the following code to display latest 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!

Leave a Reply

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