我们目前在<a href>标签内的主页上的每个帖子上显示了我们的Disqus评论计数,我们看到这是由一些javascript更新的,它会检测链接上是否存在#disqus_thread.
我们如何在标签之外显示评论计数?
这可能吗?
我们对评论的直接链接不感兴趣,所以我们想要删除链接,只显示计数.
Rya*_*n V 34
2014年11月3日更新:
我们现在有一种方法可以对任何你想要的元素使用注释计数.如果您执行以下操作,则常规count.js脚本现在可以使用:
disqus-comment-countAND类data-disqus-urlOR data-disqus-identifier属性所以现在这些元素中的任何一个都可行:
<span class="disqus-comment-count" data-disqus-url="http://example.com/path-to-thread/"> <!-- Count will be inserted here --> </span>
和
<span class="disqus-comment-count" data-disqus-identifier="your_disqus_identifier"> <!-- Count will be inserted here --> </span>
旧答案(不要再这样做了)
当涉及到它所寻找的标签类型(它必须是a标签)时,count.js脚本相当不灵活,因此您需要使用API来完成此任务.
此API调用为您指定的任意数量的线程返回一个线程数据数组(您正在寻找"Posts"整数):http://disqus.com/api/docs/threads/set/
由于API限制,您最好运行此服务器端并缓存计数以便为客户端提供服务.但是,除非你有一个非常繁忙的网站,否则做客户端通常很好.如果您的申请需要超过1000个请求/小时,您可以发送电子邮件至developers@disqus.com.
编辑
这是一个快速示例,说明如何使用jQuery执行此操作.这假设您有几个空div,如下所示:
<div class="my-class" data-disqus-url="http://example.com/some-url-that-matches-disqus_url/"></div>
Run Code Online (Sandbox Code Playgroud)
le javascript:
$(document).ready(function () {
var disqusPublicKey = "YOUR_PUBLIC_KEY";
var disqusShortname = "YOUR_SHORTNAME";
var urlArray = [];
$('.my-class').each(function () {
var url = $(this).attr('data-disqus-url');
urlArray.push('link:' + url);
});
$('#some-button').click(function () {
$.ajax({
type: 'GET',
url: "https://disqus.com/api/3.0/threads/set.jsonp",
data: { api_key: disqusPublicKey, forum : disqusShortname, thread : urlArray }, // URL method
cache: false,
dataType: 'jsonp',
success: function (result) {
for (var i in result.response) {
var countText = " comments";
var count = result.response[i].posts;
if (count == 1)
countText = " comment";
$('div[data-disqus-url="' + result.response[i].link + '"]').html('<h4>' + count + countText + '</h4>');
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18294 次 |
| 最近记录: |