Twitter has released their official share buttons with counters. In this tutorial we will discuss on how to add this Twitter Tweet button to Blogger.
Update on 5/2012: In this new tweet button, you can customize the look, mention yourself and can also mention related twitterer whom you may want to be followed by others. So have a look at the live example below by tweeting it.

Adding Twitter Button to Blogger
Step 1: Go to Blogger’s Template tab, click “Edit HTML” and tick “Expand widget Templates”.
Step 2: Find closing </body> tag and before it add:
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>I have selected to add the Twitter script before closing body for performance and to avoid multiple loading. For example, it is not needed to load the JavaScript API for every buttons shown on a single page, or, if you want to show both Tweet and Twitter Follow buttons.
Step 3: Find <data:post.body/> and immediately above or below it (or both) add any one of the code mentioned below.
Tweet Buttons for Blogger
Start by typing your USERNAME:
Vertical button
<b:if cond='data:blog.pageType != "static_page"'> <a class='twitter-share-button'data-count='vertical'data-via='blogger_Raju' expr:data-text='data:post.title' expr:data-url='data:post.canonicalUrl' href='http://twitter.com/share'>Tweet</a> </b:if>
Horizontal button
<b:if cond='data:blog.pageType != "static_page"'> <a class='twitter-share-button'data-count='horizontal'data-via='blogger_Raju' expr:data-text='data:post.title' expr:data-url='data:post.canonicalUrl' href='http://twitter.com/share'>Tweet</a> </b:if>
Tweet Button with No Count
<b:if cond='data:blog.pageType != "static_page"'> <a class='twitter-share-button'data-count='none'data-via='blogger_Raju' expr:data-text='data:post.title' expr:data-url='data:post.canonicalUrl' href='http://twitter.com/share'>Tweet</a> </b:if>
Official Tweet button as Text Link
In the official page of tweet button you will find that there are different button styles and all of them uses the Twitter JavaScript API. If you want to use text only link to share your tweet without any use of the script, then try this one.
<div id='tweet-button'>
<a expr:href='"http://twitter.com/share?url=" + data:post.canonicalUrl + "&text=Currently reading: " + data:post.title + "&via=blogger_Raju"' target='_blank'>Tweet</a>
</div>For twitter text link, you may want to add some style to it. So find for ]]></b:skin> and above it add:
#tweet-button a {
display: inline;
padding: 2px 5px 2px 20px;
background: url(http://twitter-icon.png) 1px center no-repeat;
border: 1px solid #ccc;
}Here is an example which can be also used to add sharing icons below Blogger posts:
Remember that you don't need any JavaScript.
Step 4: Save your template.
Customizing Attributions
While adding your Twitter button you would like to change some attributions. Let's have a look at these:
data-url– It’s the URL of your post which will be fetched automatically. Don't modify anything here.data-via– This is where you should be using your username. Replace blogger_Raju with your twitter Username.data-text– This is usually the title of the post.data-related– It will add any related user.data-count– The type of button you want to have.data-size- It controls the size of the button. Example:<a class='twitter-share-button' data-count='horizontal' data-via='blogger_Raju'
data-size='large' expr:data-text='data:post.title' expr:data-url='data:post.canonicalUrl' href='http://twitter.com/share'>Tweet</a>
Points to note:
Conditional tag marked in blue is used to show this button on homepage and every other page excluding static pages. If you want to show it on post pages only, then use:
<b:if cond='data:blog.pageType == "item"'> {Tweet button code goes here} </b:if>
Otherwise, delete it. That’s all folks.
Good sharing.... Thats the qoute.....
ReplyDeleteI tried the Text version but the all the posts disappeared. Is there any work around? I just want links to be shortened. Thanks.
ReplyDeleteEarl, I've changed the above code a little bit, now give it a try, it will surely work now.
ReplyDeleteThanks a bunch! Works like a charm! Do you also have any idea on how to fix Facebook link share? It's presents a horrible link on Facebook book instead of a snippet of the post and an image.
ReplyDeleteThanks again!
It's totally depends on Facebook, there's nothing I can do about it. However, you can use paragraph tags (<p> </p>) for the first paragraph of your post to pull out a small description from it. For further assistance, see my posts on Facebook and reply there to avoid off-topic discussions.
ReplyDeleteHow do I get rid of via Raju?
ReplyDeletePlease read the full post. Reading Customizing Attributions part will resolve your problem, i.e, change blogger_Raju with your's...
ReplyDeleteThanks for the tutorial! What if I don't have a Twitter account? What would I replace the "data-via" with?
ReplyDeleteCreating a twitter account will increase the visibility of you and your blog. If you don't have account then you don't need data-via='Your-User-Name', you can safely delete it.
ReplyDeletehi!
ReplyDeleteMy counts are showing tweet count are showing zero every time even if i tweet them.
Please provide your blog's URL.
ReplyDeletecan you please help me? everything is doing fine. I followed your instructions to add twitter, google plus and facebook on my blog. but it only shows on the main page not on the post pages. I tried tweaking my xml but it still doesn't show :(
ReplyDeleteHere's my blog http://oh-wheezers.blogspot.com
This can be the fault of improper use of conditional tags for Blogger. Read the given link carefully and edit the tags as necessary. This should solve your problem.
ReplyDelete@raju, thanks but that's what I did. with or without the conditional tags. For my index page (the one that works) I use static, I figured that post pages use item. Is there any way to fix this?
ReplyDeleteTry this tag:
ReplyDelete<b:if cond='data:blog.pageType != "static_page"'>
[rest of the codes goes here]
</b:if>
Remember that there should be no other conditional tags except the tags I mentioned above.
That's what I did but it does not work. But after tweaking a few more, I've finally managed to make it work. I realized a conditional tag was existing to where I pasted the code.
ReplyDeleteTherefore, I placed the code (without the conditional tag) under the first and under the 'else' of
Thank you Raju for your time to help me. More power to you :D