Blogger Ever

Get Disqus comment counts using two different methods

Disqus comments have API as well as easy to use scripts to make some plugins. In this post we are going to use two different methods to get the comment count for the disqus thread.
Get Disqus comment counts using two different methods

Disqus is one the most popular third party commenting system available today on web. I do prefer it due to its functions and large API for the developers. Disqus have Discovery feature for earnings too, even more it allows a large variety of developers' tools in order to create some useful plugins.

Same we are going to use API to fetch the comment counts as well as the second method is available which is quiet easy but however, both methods have some cons and pros So lets begin with it.


Method - 1 (The easier one)

According to my knowledge, all the Blogger (CMS) users do not have to use this because, when you add Disqus in Blogger due to default settings number of counts displays on post pages. So Blogger users are no need to follow it as it is already present in their templates. Lets move on, the people uses CMS other than Blogger can follow the procedure below.

Add the following JS to import the count.js in your template, before </head>

<script type="text/javascript">
var disqus_shortname = 'bloggerever'; // required: replace example with your forum shortname
(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = 'http://' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
Make sure to change your Disqus shortname with bloggerever.

Now wherever you want to display the comment counts, add #disqus_thread with the link in a tag. Example given below:
<a href='http://example.com/article#disqus_thread'></a>
You will see something like this:

Change comment text

The comment tag text can be changed by changing the Disqus settings. For that follow the steps below:
  • Go to Disqus dashboard > Settings
  • Change the Comment Count Link settings
Change the above field settings for your desire text to show in comment count link. Further customization can be done by CSS.

Method - 2 (Complicated one)

As I mentioned above Disqus have an API that enable developers to do more. On the same note we are going to create a sort of application that will fetch the number of comments. I highly prefer this method to those who can perform this one easily as this method can be more professional and helpful in many ways.

Lets begin by creation of an application which will give us a secret key and a public key. We are going to use public key to get the comment count. Follow the steps below to create an application.

  • Login to Disqus
  • Go to Application page
  • and click Register new application.
  • Fill the form and register your application
After registration, again go to Application page and their you can see a link that says 'configure a trusted domain', click on that link to get a public API because from those domains only the request can be send (for some security and privacy issues).

Clicking link will get you to the settings and scroll down to domain text box and write the trusted domains. Note that Disqus allows 1000 requests per hour, you can increase the limit by contacting them. After saving the domains you will get public API.

Use the below jQuery script to get the comment count using API key:
<span class='comment-count' data-disqus='http://any-disqus-thread.com'></span>
$(function () {
      var disqusPublicKey = "Disqus_Public_Key";
      var disqusShortname = "Disqus_Shortname";
      var threadUrl = 'link:' + $(',comment-count').attr('data-disqus');
      $.ajax({
          type: 'GET',
          url: 'https://disqus.com/api/3.0/threads/set.jsonp',
          data: {
              api_key: disqusPublicKey,
              forum: disqusShortname,
              thread: threadUrl
          },
          cache: false,
          dataType: 'jsonp',
          success: function (result) {
              if (result.response.length === 1) {
                  $('.post-comments').text(result.response[0].posts);
              } else {
                  $('.post-comments').text('No Comments');
              }
          }
      });
  });
Replace the highlighted code with their respective values to get the Disqus comment count. 
Blogger

2013-2016 Copyright Blogger E\ver . Coded, managed and founded by Mohammad Hamza (@hamzadhamiya) . Hosted on Blogger and fonts used are Raleway and Open Sans.