Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting DISQUS comment count

Tags:

web

disqus

I'm trying to get the comment count from a comic on my comics website. For example, comic id 66 has 2 comments. I'd like to get that count and display it on another page. So far when I follow the disqus guide below, it gives me a link to the comic with the comments, but doesn't give me the total comments.

DISQUS says...

Append #disqus_thread to the href attribute in your links. This will tell Disqus which links to look up and return the comment count. For example: <a href="http://foo.com/bar.html#disqus_thread">Link</a>.


But how would I get that count if my URL string was like this:

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

So my questions are:

  • Where would I append #disqus_thread?

  • How can I get the comments count from that one comic URL and display those total comments on another page?

like image 483
user3871 Avatar asked Jan 14 '23 15:01

user3871


2 Answers

Example here: http://help.disqus.com/customer/portal/articles/1131783-tutorial-get-comment-counts-with-the-api

Variables

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

Making the API Request

$('#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>');

      }
    }
  });
});
like image 140
Murali Bala Avatar answered Jan 16 '23 04:01

Murali Bala


While this is an old thread it looks like no answer was accepted so will add my thought incase it helps others.

In your function.php add the following:

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

Then on any page you want comment counts to appear add the following:

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

Be sure you add that in "the loop" and replace myexampleblog with your disqus account short name. Also in your Disqus account you can see what wording to use such as "0 Comments", "1 Comment", "3 Comments" etc.

like image 31
cchiera Avatar answered Jan 16 '23 03:01

cchiera