Create a Twitter Box in Your Sidebar – Part II
My Sidebar Twitter box tutorial seems to have struck a chord with WordPress users and it has generated some interesting questions. One of them, from TheNext2ShineBlog posed an interesting problem I decided to look into in more detail:
the only thing I would like to change is the time aspect (23 days ago // 4 hours ago). Is there a css code to hide that link without taking away the links from the original twitter message?
What TheN2S is refering to is the tail end of each Twitter message that reads either “less than a minute ago”, “a few minutes ago” etc up to “X days ago”.
Careful inspection of the JavaScript that generates the Tweets for the application (found here) shows that the time information is a core function of the Twitter system so it is coded into the main structure of the application itself. Therefore it is hard to siply remove it unless you want to create your own custom JS. But TheN2S is on the right track in asking if it can be removed by way of CSS.
Lifting a random tweet off my own site I found that the main body the JS spits out is contained within a span tag while the tail end with the time info is not:
<li> <span>@<a href="http://www.twitter.com/webb_art">webb_art</a> DropBox works well for me and is platform independent: <a href="http://www.getdropbox.com/"> http://www.getdropbox.com/</a></span> <a href="http://twitter.com/mor10/statuses/1003495851" style="font-size: 85%;"> about 15 hours ago</a> </li>
That means we can use CSS to hide the content not in the while maintaining the visibility of the content that is. That requires some additions to the original CSS code:
#twitter_div ul li span { visibility: visible; } #twitter_div ul li span a { color: #D78E42; visibility: visible; } #twitter_div ul li a { visibility: hidden; }
The first two single out the regular and link contents within the span specifically and set their visibility to visible. This is done because the last style sets the visibility of all anchors within list items under the twitter_div ID to hidden. So we are really working backwards – first hiding everything and then unhiding it in particular cases.
By adding these three style elements the time information will be hidden by the CSS while everything else shows up normally.
CSS Gallery Features
After three days in it’s new incarnation, Design is Philosophy is being featured in a myriad of CSS and web galleries. this in turn caused an insane spike in readership that makes my stats page look pretty weird. I haven’t had time to track back all the postings yet so I’ll keep adding on to this list in the coming days. Anyways, here is an incomplete list with big thanks to the galleries for featuring me and all the people who vote for my site (yes, you should vote too):
- CSS Mania
- CSS Based
- CSS Snap
- CSS Container
- One CSS
- CSS Chick
- CSS Leak
- Most Inspired
- CSS Brigit
- CSS Mix
- WeLoveWP
- RGB Garden
- CSS Breeze
- CSS Season
Feel free to submit this site to other CSS and web galleries if you like. And a big thanks to those of you who allready have.
Create a Twitter box in your sidebar
As part of the new design for this blog I added a Twitter box in the sidebar. There are hundreds of WordPress plugins that do this for you but they are all quite involved and they bog down the blog unnecessarily. Much easier to just hard code the box into your blog yourself. It’s actually surprisingly easy to do, but the code that Twitter provides is a bit wonky (and shockingly it doesn’t validate!).
In this tutorial you’ll learn how to easily create a fancy box that displays your latest Twitter rants in your sidebar without having to turn to clunky plug-ins that bog your blog down.
Get your Twitter Badge
To start off with you need your Twitter Badge. This is the official Twitter JavaScript that passes your latest tweets to wherever you want. You can get your badge at twitter.com/badges. Twitter has custom badges for MySpace, Blogger, Facebook and TypePad but surprisingly nothing for WordPress. That doesn’t really matter because it’s actually easier to just make one of your own.
Select Other and you are taken to page two which presents three options:
- Flash (Just Me) – hideous
- Flash with Friends – even more hideous
- HTML/JavaScript – great for people with eyes
Select the last option (HTML/JavaScript) and you are taken to page 3 where you can customize the code by defining how many tweets you want and what the Badge title should be. I chose 2 tweets and turned the title off. This provided me with the following code:
<div id="twitter_div"> <ul id="twitter_update_list"></ul> </div> <script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script> <script type="text/javascript" src="http://twitter.com/statuses/user_timeline/mor10.json?callback=twitterCallback2&count=2"></script>
(This code will of course differ depending on your settings.)
Paste the code in wherever you want on your site or your blog.
Style your Badge
The Twitter Badge comes equipped with JavaScript that injects the tweets into your badge in the form of unordered list items as well as built in style elements. They are: #twitter_div (styles the badge wrap), .sidebar_title (styles the title) and #twitter_update_list (styles the unordered list).
Before you start styling, you have to fix the generated code to make it validate. Since it is the JavaScript that actually generates the list items, browsers and validators get all cranky about the code because there are no list items within the unordered list in the markup itself. Therefore you need to insert an empty list item just to please the W3C gods:
<div id="twitter_div"> <ul id="twitter_update_list"> <li></li> </ul> </div> <script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script> <script type="text/javascript" src="http://twitter.com/statuses/user_timeline/mor10.json?callback=twitterCallback2&count=2"></script>
Once that’s settled you can start styling your elements. Again because of the JavaScript you have to stick with the names Twitter provides, but that shouldn’t cause any problems. For my Twitter Badge I created a background PNG much larger than what I actually needed to accommodate a future situation where I would add more than 2 tweets at a time (which you can see by clicking here). The background graphic is cut off by a matching blue bottom border. The whole style package looks like this:
#twitter_div { background-image: url('img/twitterBG.png'); background-repeat: no-repeat; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #5AA5BC; font-family: Arial, Helvetica, sans-serif; font-size: 0.9em; margin-top:10px; padding-top: 30px; padding-right: 5px; padding-left: 5px; } #twitter_div ul li { color: #0C93BA; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #A1E8F7; } #twitter_div ul li a { text-decoration: none; color: #DDA84E; } #twitter_div ul li a:hover { text-decoration: none; color: #D78E42; } #twitter_div p { text-align: right; padding-right: 6px; padding-bottom: 10px; }
Add a link for future followers
The observant reader will notice that there’s still one element missing: The Follow me on Twitter link in the bottom right corner. That’s the reason for the p style in the CSS code as well. So with the follow link, the final HTML markup looks like this:
<!-- TWITTER --> <div id="twitter_div"> <ul id="twitter_update_list"> <li></li> </ul> <p><a href="http://twitter.com/mor10" title="Follow me on Twitter" target="Twitter">Follow me on Twitter</a></p> </div> <script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script> <script type="text/javascript" src="http://twitter.com/statuses/user_timeline/mor10.json?callback=twitterCallback2&count=2"></script> <!-- END TWITTER -->
And with that you have a fancy Twitter box you can put in your sidebar without bogging your blog down with plug-ins.
Read the second half of this tutorial, in which you learn how to hide the time stamp at the tail end of the Tweets.

.jpg)



