user3871
user3871

Reputation: 12718

Getting DISQUS comment count

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:

Upvotes: 4

Views: 11285

Answers (4)

jeff
jeff

Reputation: 1

I had this issue and it was so annoying for so long, i finally solved it with this simple code where i wanted the comments to show to force it to append the #disqus_thread to the end:

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

Upvotes: 0

Murali Bala
Murali Bala

Reputation: 1143

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

      }
    }
  });
});

Upvotes: 4

TechRemarker
TechRemarker

Reputation: 2938

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.

Upvotes: 3

Matt Stephens
Matt Stephens

Reputation: 922

If you have Disqus comments enabled on that page then you just need a link to that page for example

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

You then amend the #disqus_thread

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

The javascript it gets you to link to checks the comments count for the page linked and the comments under #disqus_thread it then overwrites the link you created below

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

With something like 1 Comment

Upvotes: 2

Related Questions