Skip to main content

Display total number of Disqus comments on a page in Drupal 7

Published on 12th June 2013

On this site I decided to use the commenting platform Disqus. I wanted to display the comment count for each page, but unfortunately the module doesn't allow this straight out of the box. To do this, follow these 3 steps.

  1. First we need to add the Disqus javascript snippet into the footer of our site. We can do this by adding the following code in your active theme's template.php file.

    <?php
    function MYTHEME_preprocess_html(&$vars) {
      $disqus_comment_count = "
      /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
      var disqus_shortname = 'example'; // required: replace example with your forum shortname
    
      /* * * DON'T EDIT BELOW THIS LINE * * */
      (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);
      }());";
      
      drupal_add_js($disqus_comment_count, 
      array(
       'type' => 'inline',
       'scope' => 'footer'
        )
      );
    }
    ?>
    

    Make sure you replace 'MYTHEME' with your theme name and 'example' with your disqus shortname. (If you don't know your Disqus shortname login to disqus > admin > settings > general > site identity)

  2. Now we need to add the following <a> tag in the node.tpl.php. The Disqus javascript looks for #disqus_thread and inserts the total number of comments into the <a> tag. You can add a class to the tag and style as you wish. You should be able to display the total comment count in any .tpl file as long as you include the Disqus javascript in the footer and <a> tag on the page.

    <?php
      <a href="<?php print $node_url; ?>#disqus_thread" data-disqus-identifier="<?php print $node->disqus['identifier']; ?>"></a>
    ?>
    
  3. Finally we need to adjust the way Disqus displays the comment count and reactions. To do this login to disqus > admin > settings > general > comment count link / reaction count link. To display just the numbers it will look something like this.

    Disqus comment count / reaction count settings