There are lots of resources out there for displaying your latest twitter posts in your website. Some of them require plugins or some may require javascript to work. After trying a few scripts I came across a little PHP script from The Lylo Files that displays up to 20 of your posts on your web site. After using it for a little while I decided to make it more functional.
This script has been updated and given a new name. Check the updated version.
The list of changes from the original are:
- #Hashtags are now click-able.
- Changed @reply to include _ underscores and – as many user-names contain these.
- Added retrieval of published date and it’s associated link.
- Split the date up so that you can have many different variants like Thu 10 Sep or Sep 10 2009, etc.
Setup
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?php
$doc = new DOMDocument();
# load the RSS -- replace 'worldoweb' with your username.
if($doc->load('http://twitter.com/statuses/user_timeline/worldoweb.rss')) {
echo "<ul>\n";
# number of <li> elements to display. 20 is the maximum
$max_tweets = 15;
$i = 1;
foreach ($doc->getElementsByTagName('item') as $node) {
# fetch the title from the RSS feed.
$tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;
#Fetch the link from the feed
$link = $node->getElementsByTagName ('link') ->item(0)->nodeValue;
# This gets the date and separates it into sections
$pubDate = $node->getElementsByTagName('pubDate')->item(0)->nodeValue;
$month = substr($pubDate, 7, 4);
$date = substr($pubDate, 4, 3);
$year = substr($pubDate, 11, 5);
$day = substr($pubDate, 0 , 3);
$title=$day.$date.$month.$year;
#pre-defined date lazy tags. You can also make your own using $month,$date,$year & $day
$ddmmyy=$date.$month.$year;
$mmyy=$month.$year;
$mmddyy=$month.$date.$year;
$ddmm=$date.$month;
# the title of each tweet starts with "username: " which I want to remove
$tweet = substr($tweet, stripos($tweet, ':') + 1);
# OPTIONAL: turn URLs into links
$tweet = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\./-]*(\?\S+)?)?)?)@',
'<a target="blank" title="$1" href="$1">$1</a>', $tweet);
#OPTIONAL: turn hashtags into links
$tweet = preg_replace('/#([0-9a-zA-Z_-]+)/',
"<a target='blank' title='$1' href=\"http://twitter.com/search?q=%23$1\">#$1</a>", $tweet);
#OPTIONAL: turn @replies into links
$tweet = preg_replace("/@([0-9a-zA-Z_-]+)/",
"<a target='blank' title='$1' href=\"http://twitter.com/$1\">@$1</a>", $tweet);
#The following line can changed to suit your needs.
echo "<li class='tweet'>"."<strong>"."<a href='$link' target='blank' title='$title'>". $ddmmyy ."</a>" ."</strong>" .$tweet ."</li>\n";
if($i++ >= $max_tweets) break;
}
echo "</ul>\n";
}
?> |
It is pretty simple to set up just copy and paste the above code into your site, change worldoweb to your twitter username and change the $max_tweets to anything between 1 and 20. The default output, in line 51, displays the link at the beginning of the tweet wrapped in bold, strong tags.
![]()
To display the date at the end, change line 51 to:
|
51 |
echo "<li class='tweet'>". $tweet . "<strong>". "<a href='$link' target='blank' title='$title'>" . $ddmmyy."</a>" . "</strong>" . "</li>\n"; |
![]()
You can of course remove the link altogether.
|
51 |
echo "<li class='tweet'>" . "<strong>" . $ddmmyy . "</strong>" . $tweet . "</li>\n"; |
The change the date format string in the tweet (line 51) you can change the default $ddmmyy to any of the following.
- To display the date like 09 Sep change it to $ddmm
- To display the date like Sep 09 change it to $mmyy
- To display the date like Sep 09 2009 change it to $mmddyy
- To add the day of the week to these use $day.$mmyy.
I hope you enjoyed this script and I would like to thank the original author of the script for making this possible. I would love to hear all of your comments so don’t be shy.


how many time we are hit a url in day or month to twiter blog.?is there any limit?
so, please reply me as soon as possible
thanks
Not to my knowledge.
Updated new script to include time since, like my sidebar. Both v1 & v2 will be available as a zipped download soon. Created a new post and updated the name.
Glad to see that it is working again.
Line 5 sorry.
It has now re-solved itself. Line 8 is line 4 in the script I think.
No as you can see my it is in my footer. Can you tell me what line 8 in your footer.php contains please?
The script worked brilliant the first couple of days. Now I’m getting this error/warning!
http://flyingdesigns.co.uk/design/weird.png
Are you getting this too?