获得DISQUS评论计数

时间:2013-04-18 05:48:24

标签: web disqus

我正试图通过comics website上的漫画来获取评论数。例如,漫画ID 66有2条评论。我想得到那个计数并将其显示在另一页上。到目前为止,当我按照下面的disqus指南时,它会给我一个漫画链接和评论,但不会给我总评论。

DISQUS says...

  

#disqus_thread附加到您链接中的href属性。这将   告诉Disqus要查找哪些链接并返回评论计数。对于   例如:<a href="http://foo.com/bar.html#disqus_thread">Link</a>


但是如果我的URL字符串是这样的话,我将如何计算:

<a href=".?action=viewimage&site=comics&id=66">Link</a>

所以我的问题是:

  • 我会在哪里追加#disqus_thread

  • 如何从一个漫画网址中获取评论,并在另一个页面上显示这些评论?

4 个答案:

答案 0 :(得分:4)

此处示例:http://help.disqus.com/customer/portal/articles/1131783-tutorial-get-comment-counts-with-the-api

变量

<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
  var disqusPublicKey = "YOUR_PUBLIC_KEY";
  var disqusShortname = "thenextweb"; // Replace with your own shortname

  var urlArray = [];
});
</script>

var urlArray = [];
//...continued from above

$('.count-comments').each(function () {
  var url = $(this).attr('data-disqus-url');
  urlArray.push('link:' + url);
});

发出API请求

$('#get-counts-button').click(function () {
  $.ajax({
    type: 'GET',
    url: "https://disqus.com/api/3.0/threads/set.jsonp",
    data: { api_key: disqusPublicKey, forum : disqusShortname, thread : urlArray },
    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>');

      }
    }
  });
});

答案 1 :(得分:3)

虽然这是一个旧线程,但似乎没有接受任何答案,所以会添加我的想法,这有助于其他人。

在你的function.php中添加以下内容:

function disqus_count($disqus_shortname) {
    wp_enqueue_script('disqus_count','http://'.$disqus_shortname.'.disqus.com/count.js');
    echo '<a href="'. get_permalink() .'#disqus_thread"></a>';
}

然后,在任何要显示评论计数的页面上添加以下内容:

<?php disqus_count('myshortcode'); ?>

请务必在“循环”中添加该内容,并将myexampleblog替换为您的disqus帐户短名称。同样在你的Disqus帐户中,你可以看到使用的措辞,如“0评论”,“1评论”,“3评论”等。

答案 2 :(得分:2)

如果您在该页面上启用了Disqus评论,那么您只需要指向该页面的链接,例如

<a href=".?action=viewimage&site=comics&id=66">Link</a>

然后修改#disqus_thread

<a href=".?action=viewimage&site=comics&id=66#disqus_thread">Link</a>

一样

它让你链接的javascript检查链接页面的评论数量和#disqus_thread下的评论然后覆盖你在下面创建的链接

<a href=".?action=viewimage&site=comics&id=66#disqus_thread">Link</a>

使用类似1 Comment

的内容

答案 3 :(得分:0)

我遇到了这个问题,这很烦人,我终于用这个简单的代码解决了这个问题,我想让评论显示强制它将#disqus_thread附加到最后:

<a href="<?php the_permalink() ?>#disqus_thread">Comments</a>