在DIV或SPAN中显示Disqus评论计数 - 而不是

问题描述 投票:13回答:2

我们目前在<a href>标签内的主页上的每个帖子上显示了我们的Disqus评论计数,我们看到这是由一些javascript更新的,它会检测链接上是否存在#disqus_thread。

我们如何在标签之外显示评论计数?

这可能吗?

我们对评论的直接链接不感兴趣,所以我们想要删除链接,只显示计数。

disqus
2个回答
34
投票

2014年11月3日更新:

我们现在有一种方法可以对任何你想要的元素使用注释计数。如果您符合以下条件,常规的count.js脚本现在可以使用:

  • 使用disqus-comment-count类AND
  • 使用data-disqus-url OR 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>

旧答案(不要再这样做了)

当涉及到它所寻找的标签类型时,count.js脚本相当不灵活(它必须是a标签),所以你需要使用API​​来实现这一点。

此API调用为您指定的任意数量的线程返回一个线程数据数组(您正在寻找“Posts”整数):http://disqus.com/api/docs/threads/set/

由于API限制,您最好运行此服务器端并缓存计数以便为客户端提供服务。但是,除非你有一个非常繁忙的网站,否则做客户端通常很好。如果您的申请需要超过1000个请求/小时,您可以发送电子邮件至[email protected]

编辑

这是一个快速示例,说明如何使用jQuery执行此操作。这假设您有几个空div,如下所示:

<div class="my-class" data-disqus-url="http://example.com/some-url-that-matches-disqus_url/"></div>

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>');

                    }
                }
        });

});

0
投票

J Kure Sono Chion:

var gettingCount = false;
var countCallerCallback = null;
function countCallback(data) // returns comment count or -1 if error
{
    var count = -1;
    try {
        var thread = data.response[0];
        count = thread === undefined ? "0" : thread.posts;  
    }
    catch (ex) {
        console.log("FAILED TO PARSE COMMENT COUNT");
        console.log(ex);
    }

    // always do this part
    var commentCountScript = document.getElementById("CommentCountScript");
    document.getElementsByTagName('head')[0].removeChild(commentCountScript);
    countCallerCallback(count);
    gettingCount = false;
    countCallerCallback = null; // if this got reset in the line above this would break something
}
function getCommentCount(callback) {
    if(gettingCount) {
        return;
    }
    gettingCount = true;

    var script = document.createElement('script');
    script.id = "CommentCountScript";
    var apiKey = "api_key=MY_COOL_API_KEY";
    var forum = "forum=MY_FORUM_SHORT_NAME"
    var thread = "thread=" + "link:" + window.location.href;
    script.src = 'https://disqus.com/api/3.0/threads/set.jsonp?callback=countCallback&' + apiKey + "&" + forum + "&" + thread;
    countCallerCallback = callback;
    document.getElementsByTagName('head')[0].appendChild(script);
}
getCommentCount(function(count){alert(count);});
© www.soinside.com 2019 - 2024. All rights reserved.