Search
Tweet Tweet
Stuff I like

See Me Play


get music on iTunes
Quantcast

« New Blog-y Blog | Main | 2x4 Ring »
Friday
Apr302010

Adding the "Like" Button to every post

I just figured out how to add the Facebook "Like" button to every post in my blog.  For those of you who use squarespace, this could be pretty useful for you. You must have jquery for this to run.

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {

$('.journal-entry-tag-post-body-line3').each(function(index) {
var url = $('.title a').get(index);

$(this).append('<iframe src="http://www.facebook.com/widgets/like.php?href='+url+'" scrolling="no" frameborder="0" style="border:none; width:450px; height:80px"></iframe>');
});

});
</script>

 

Reader Comments (41)

I tried but am so bad w/ code. You'll have to help :) I'll trade for a yummy meal!

April 30, 2010 | Unregistered CommenterBendistraw

You'll need to make sure you have jquery first
<p><script src="http://code.jquery.com/jquery-latest.js"></script></p>

May 1, 2010 | Registered CommenterMarissa

Great guide! Quick question... do you put this code on every blog post, the header/footer of the Journal configuration or in your site header/footer?

May 3, 2010 | Unregistered CommenterJosh Braaten

If you want to use this method to apply this to non-Journal entry pages you will need to change this line of code:

var url = $('.title a').get(index);

to this:

var url = $('.active-module a').get(index);

Basically, this is grabbing the URL for the page that is being viewed.

June 2, 2010 | Unregistered CommenterKris Black

Oh, I forgot to add that you will need to change where to add the Facebook Like button. Since you won't be adding it to a Journal Entry page you will need to change this:

$('.journal-entry-tag-post-body-line3').each(function(index) {

to add the link to another common CSS selector in your page layout. For a site I created I just added an HTML widget with a <div> that had a unique id like this:

<div id="facebook-like"></div>

then change the script to this:

$('.facebook-like').each(function(index) {

June 2, 2010 | Unregistered CommenterKris Black

Hi, thanks for posting this!

Is the extra height (80 pixels) to alow profile pictures to display? What happens to extra pictures that dont fit in the space, do they just get cut off? ( that's what I hope happens) I dont have enough friends liking stuff to see how that part works :^)
cheers!

June 4, 2010 | Unregistered Commenterrates

Hi Marissa! Thanks for this little trick. I've been looking around for ages trying to find this solution.
Just wondering though, is there a way to get the button to appear above the 'tag' section and with some space between them?
Thanks again :)

June 5, 2010 | Unregistered CommenterJason Smith

Hey Jason, you can definitely move it to wherever you need it to be. To put it above the tags you should actually append it to the body of the journal entry. You can do that by subbing $('.journal-entry-tag-post-body-line3') for $('.journal-entry .body') and it will put your iframe at the bottom of your journal entry. You can see that I've switched it out. Good luck!

June 5, 2010 | Registered CommenterMarissa

Thanks heaps Marissa :)
Just got another question though... when I insert the code it doesn't seem to make the Like Button post specific, i.e Every Like button on every one of my blog posts reads exactly the same and isn't individual for each post, on your blog posts for exapmle, some posts have 10 'likes' and others have 3 'likes', my ones are all the same number of like for every post.
Is there a way to get around this?
Let me know if you don't understand, Im not the best at explaining things! :)
Thanks

June 6, 2010 | Unregistered CommenterJason Smith

this is great- thank you for sharing, i found the link via the squarespace mans. just a question if anyone else has encountered the problem i'm seeing. which is that when someone "likes" an entry on my blog i'm seeing something like "36,537 people like this" which clearly is not true. so i'm wondering if this is some kind of facebook bug or perhaps something i'm doing wrong w/ the implementation?

any ideas much appreciated. thanks!

June 14, 2010 | Unregistered CommenterHandH

Hey HandH -
are you sure the link that you are "liking" is your blog and not just squarespace in general? Can you post a link to your site?

June 14, 2010 | Registered CommenterMarissa

hi m- thanks so much for replying! i've set the code in the header for the top html section of the site. perhaps i've done this incorrectly though...the website is:
http://hydroxandhorlix.squarespace.com/

the journal is actually nested in an html page. i'm not certain how to place code in just the header for the journal itself though...any ideas?

June 15, 2010 | Unregistered CommenterHandH

Thank you very much for that great tip, worked like a charm. My only question is how to right justify it? Right now it is defaulted to go on the left. Either way thank you, worked great.

June 19, 2010 | Unregistered CommenterMaxwell

Hey HandH - it looks like when I hit the "like" button on your page it said "Marissa likes http://ilovehotdogs.net/ on ilovehotdogs.net." so obviously something isn't quite right. You're having a problem with the line

var url = $('.title a').get(index);

I would do an alert(url); to see what's happening there and see if you can figure out whether you've put your code in the right place.

June 23, 2010 | Registered CommenterMarissa

Hey Marissa,

This is great. Thanks for posting. I've already added it to a couple of my sites.

I had a question about how you positioned the button to show up below the posts. A client of mine wants to add some google ads below his posts and I'm not quite sure how to accomplish that. Could you help?

the site is www.foodzine.gr and any help is appreciated.

Davy

June 23, 2010 | Unregistered CommenterDavy Rudolph

Hey Davy,
so you could probably use the same thing. This is just basic javascript appending. So like I did with mine (subbing $('.journal-entry-tag-post-body-line3') for $('.journal-entry .body') and it will put your iframe at the bottom of your journal entry.) to put it in the journal entry itself instead of the post details. You would just swap out the facebook iframe for whatever other code you want.

June 24, 2010 | Registered CommenterMarissa

Marissa, much obliged for sharing this code with Squarespace users. I'll be sending friends here to get the code too!

P.S.

I find this blog post via this discussion thread:

http://developers.squarespace.com/design-coding/post/1080044

Is there any way you can get the block 'Like' button on your posts instead of the sentence? I prefer that. Thanks

June 24, 2010 | Unregistered CommenterJonny Rowntree

I found you need to adjust the code to have the Like for each separate blog entry:

This section needs to look like this:

$('.journal-entry-tag-post-body-line3').each(function(index) {
var url = $('.journal-entry .title a').get(index);

it's working on a website for a team I run: http://www.solobreakracing.com.

If you don't, what happens is if you have other links on your website, you're actually apply the Like to that website and not the actual blog summary.

June 24, 2010 | Unregistered CommenterRick

Hi Marissa -- I found my way here from a SS twitter post :) I just wanted to let you know that it implemented seamlessly into my site and RIck's suggestion was also helpful to get it going for each individual blog summary.

My question is similar to Jonny Rowntree -- is there any way to get the more simple "like box" without profile pictures? I would love to cut down on the space this takes up in the page and this seems like a good solution for me.

Any advice you can give is appreciated! This is a great post and a solution I have been looking for forever!

June 24, 2010 | Unregistered CommenterChristina

@Rick and @Marissa

I came back here because the like button is appearing on my main blog (journal) page with either Marissa's or Rick's code in place. However, when I click on an individual blog post, there is no like button at the bottom of that individual post page with either Rick or Marissa's code.

Is it possible to get the like button to appear on individual blog posts?

Thank you very much....

Never mind my question! You know how it is, you write the question, then it helps you find the answer yourself. Instead of putting the code in the blog (journal) header I put it into the code injection point in the home page in website settings. Now it's working on both the blog page and the individual blog entry!

Thank you rick for the tip. I will update with that info.

June 25, 2010 | Registered CommenterMarissa

Ya know, I was very curious to use this, but you're explanation helps nothing. Sure you provide some code, but you fail to mention where to put it. Does it have to be in the html of every post? In one of the customize sections of my Squarespace site?

It's really unclear what the heck any of this is talking about.

June 25, 2010 | Unregistered CommenterDane Deasy

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>