Snippet: Display your latest tweets without any plugin

Add following snippet where you want to show your tweets

Usage

It will fetch my last five tweets.
echo get_my_tweets('my_screen_name', 5);

Snippet

function get_my_tweets($screen_name, $max_items=5) {
    include_once(ABSPATH . WPINC . '/feed.php');
    $rss = fetch_feed('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name='.$screen_name);
    $maxitems = $rss->get_item_quantity($max_items);
    $rss_items = $rss->get_items(0, $maxitems);

    $tweets = '<ul>';
    if ($maxitems == 0) $tweets .= '<li>No items.</li>';
    else {
        // Loop through each feed item and display each item as a hyperlink.
        foreach ( $rss_items as $item )
            $tweets .= '<li><a href="'.$item->get_permalink().'">'.$item->get_title().'</a></li>';
    }
    $tweets .= '</ul>';
    return $tweets;
}

Result

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>