问题
Say I have a blogging site http://www.example.com
using custom cms, and I want each content in the site to display a like button. I have been able to add a mod that inserts code snippets in each page, incliding the like code, but how do I go about differentiating the likes from each different post because as I recall, the facebook like code requires a src="http://www.example.com"
, how do I make the src
value unique for each page like src="http://www.example.com/news/a_news.php"
. How do I achieve this...?
回答1:
refer below URL to generate like button for your page. http://developers.facebook.com/docs/reference/plugins/like/
Once you are done with settings get Code (SDK script + button code) which looks like below.
like button code for URL http://www.site.com/news/a_news.php
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-like" data-href="http://www.site.com/news/a_news.php" data-send="false" data-layout="box_count" data-width="450" data-show-faces="true"></div>
now you have to dynamically set up like button code for each link.
say for b_news.php button will be
<div class="fb-like" data-href="http://www.site.com/news/b_news.php" data-send="false" data-layout="box_count" data-width="450" data-show-faces="true"></div>
for c_news.php button will be
<div class="fb-like" data-href="http://www.site.com/news/c_news.php" data-send="false" data-layout="box_count" data-width="450" data-show-faces="true"></div>
you just have to change data-href= in like button and all set.
来源:https://stackoverflow.com/questions/7980943/inserting-facebook-like-button