【问题标题】:Showing Disqus comment count in a DIV or SPAN - not <a href>在 DIV 或 SPAN 中显示 Disqus 评论计数 - 不是 <a href>
【发布时间】:2013-04-21 01:44:25
【问题描述】:

我们的 Disqus 评论计数目前显示在我们主页上每个帖子的 &lt;a href&gt; 标记内,我们看到这是由一些检测链接上是否存在#disqus_thread 的 JavaScript 更新的。

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

这可能吗?

我们对直接链接到 cmets 不感兴趣,因此我们想删除该链接并仅显示计数。

【问题讨论】:

    标签: disqus


    【解决方案1】:

    2014 年 11 月 3 日更新:

    我们现在有一种方法可以对您想要的任何元素使用评论计数。如果您满足以下条件,那么常规的 count.js 脚本现在可以工作:

    • 使用 disqus-comment-count 类 AND
    • 使用data-disqus-urldata-disqus-identifier 属性

    所以现在这些元素中的任何一个都可以工作:

    &lt;span class="disqus-comment-count" data-disqus-url="http://example.com/path-to-thread/"&gt; &lt;!-- Count will be inserted here --&gt; &lt;/span&gt;

    &lt;span class="disqus-comment-count" data-disqus-identifier="your_disqus_identifier"&gt; &lt;!-- Count will be inserted here --&gt; &lt;/span&gt;

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

    count.js 脚本在查找标签类型时相当不灵活(它必须是a 标签),因此您需要使用 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>
    

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

    【讨论】:

    • 干杯瑞恩。我觉得很奇怪为什么它必须是一个 A 标签——对我来说似乎是一个很大的疏忽。我们的网站今天接待了大约 25-30,000 名访问者,所以是的,它的流量非常高 - 这可能会将其归入每小时 1000 多个请求的类别?它昨天的页面浏览量为 73k。
    • 给我们发电子邮件至developers@disqus.com - 我们可以达到极限,我们只是想确保我们已经取得联系以确保一切顺利:-)
    • 嗨,Ryan,我们刚刚尝试发送电子邮件,但我们收到了回复,说我们没有权限或 Google 群组不存在?
    • 对不起,然后试试 ryan@disqus.com
    • 目前显示“# Comments”,但有没有办法只显示帖子数? disqus.com 上的文章提到要编辑设置>常规,但这不会删除所有其他实例的“评论”一词吗?
    【解决方案2】:

    没有 jQuery 解决方案:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-11
      • 2014-02-13
      • 2013-08-07
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多