如何使用HTML5获得Facebook评论计数



我知道两种方法可以检索Facebook上的评论数量:

<fb:comments-count href=http://example.com/></fb:comments-count> awesome comments 

和。。。

<iframe src="http://www.facebook.com/plugins/comments.php?href=example.com&permalink=1" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:130px; height:16px;" allowTransparency="true"></iframe> 

但是没有一个是HTML5解决方案的语义,还有其他选择吗?

您也可以通过以下方式获取HTML5有效的FB注释计数:

<div class="fb-comments-count" data-href="/post URL">0</div>

FB会自动将"0"替换为正确的数字。如果您在那里放置诸如"很棒的评论"之类的内容,它将被替换为评论数量,即"13"等。

为了获取数字并使用您自己的文本,请执行以下操作:

<a href="/post URL"><div class="fb-comments-count" data-href="/post URL">0</div> awesome comments</a>

请注意,您必须将类包装在div 中,否则注释计数器将不起作用。

使用 jquery 的图形 API 示例:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    $.ajax({
        url: 'http://graph.facebook.com/http://www.google.com',
        dataType: 'jsonp',
        success: function(data) {
             alert("comments: " + data.comments);
       }
    });
});
</script>

v2.3 开始,它可以由 span 处理,它已经具有原生display: inline;语义。

<a href="/post-link">
  <span class="fb-comments-count" data-href="/post-link">0</span> comments
</a>

对于 wordpress 帖子,您可以像<fb:comments-count href=<?php the_permalink();?>></fb:comments-count> awesome comments

最新更新