Comment count is shown in the $links variable in node.tpl.php, which is fine for most I suppose.

But $links isn't very flexible, and it would be really nice to have this as a stand-alone variable in node.tpl.php. A disqus_preprocess_node() should be able to do this.

Note to self: cook up a patch that introduces this.

Comments

meramo’s picture

Subscribing

noslokire’s picture

Subscribing..

jumpfightgo’s picture

Unfortunately the number of comments isn't store anywhere server-side so all we can do is print the theme function that will use javascript to replace the link "Comments" with "X Comments". (That's how the existing comments link works in the $links variable.)

<?php print theme('disqus_comments_num', $SHORTNAME, 'node/'. $node->nid, t('Comments')); ?>

To do more I think we need to dig into drupal behaviors in disqus.js

teastburn85’s picture

Also, I think the it was implemented with JS because it most systems it will ruin caching if the comment counts keep changing in the page HTML. This is not the case if your caching is strictly time based.

frob’s picture

subscribling

espenmoe’s picture

subscribing

erdembey’s picture

subscribing for D7

kenneth@fiil.eu’s picture

Subscribing

erdembey’s picture

Let me provide a bad solution for this :) ;

In drupal 7 i can manage to isolate the comment count in $links with hiding the other variables in $links.
For example;

      hide($content['links']);
      hide($content['links']['statistics']);
      hide($content['links']['node']);

With this method i can prevent the read count and read more text appearing with comment count.
After that i can use

      print render($content['links']);

without a problem.

Btw. i see 0 comments where it should be 0 comment (isn't it?) and also it seems localization is not available in this context.

datawench’s picture

Is this theming function an incomplete feature? I don't see anything in the function that actually pulls the number of comments from the API. It seems to just render a linked bit of text.

Poieo’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Category: feature » support

I was using the following successfully in Drupal 6. Doesn't anyone know the equivalent for Drupal 7?

<?php print theme('disqus_comments_num', $domain, $path, t('Comments'), "node/$node->nid"); ?>
ryan.gibson’s picture

Yea, this is what I'm looking to do as well, except I'm looking to add the Disqus comment count as a custom Display Suite field.

Poieo’s picture

This is what I ended up using in D7:

<a href="<?php print $node_url; ?>#disqus_thread">Comments</a>

The disqus script converts it into 'X comments and Y reactions'.

@ryanissamson - You might try http://drupal.org/project/computed_field with the above code to us it in display suite.

ryan.gibson’s picture

Poieo, thanks for the info. I'm wondering, why would I need the computer field module? Couldn't I just add a custom php field in DS display using the php filter?

I've actually given that a shot, it doesn't seem to be working just yet.

heshanlk’s picture

I did this. This may helpful.

/**
 * Implements hook_node_view().
 */
function MYMODULE_node_view($node, $view_mode) {
  if (isset($node->disqus) && user_access('view disqus comments') && $node->disqus['status'] == 1) {
    switch ($view_mode) {
      case 'full':
        // Display the Disqus link.
        $links['disqus_comments_num'] = array(
          'title' => t('Comments'),
          'href' => 'node/' . $node->nid,
          'fragment' => 'disqus_thread',
          'attributes' => array(
            // Identify the node for Disqus with the unique identifier:
            // http://docs.disqus.com/developers/universal/#comment-count
            'data-disqus-identifier' => 'node/' . $node->nid,
          ),
        );
        $node->content['links']['disqus'] = array(
          '#theme' => 'links',
          '#links' => $links,
          '#attributes' => array(
            'class' => array('links', 'inline'),
          ),
        );

        // Attach disqus.js to load the Disqus comment count JavaScript.
        $node->content['links']['#attached']['js'][] = drupal_get_path('module', 'disqus') . '/disqus.js';
        $node->content['links']['#attached']['js'][] = array(
          'data' => array('disqusComments' => $node->disqus['domain']),
          'type' => 'setting',
        );
        break;
    }
  }
}
dsbrianwebster’s picture

I agree this should be available as a field, not in the links variable on the teaser.

Here's a solution I'm using in the meantime using a display suite code field...

  1. create a code field using the Display Suite module. "Display Suite Comment Count", for example.
  2. add the snippet below to the "field code" textarea. Be sure the text format is set to "Display Suite code" and the Token checkbox is turned on.
  3. save the field, go to the "manage display" tab for your content, enable a display suite layout for your view mode, and you will see your new "Display Suite Comment Field" which you can place anywhere you like

Field code snippet:

<?php
drupal_add_js(drupal_get_path('module', 'disqus') . '/disqus.js');  
drupal_add_js(array('disqusComments' => $entity->disqus['domain']), 'setting');
?>
<a href="[node:url]#disqus_thread" data-disqus-identifier="node/[node:nid]">Comments</a>
Grayside’s picture

I rolled a partial patch as part of https://drupal.org/node/590582#comment-7760907. I thought the issues were explicitly for different versions :/

kclarkson’s picture

@Delicious Simplicity This is GREAT !!! Thank you so much !

Is there some way to get this added as an additional module to disqus? I am sure many people would like to see this feature.

lionelmes’s picture

It has information I have been searching for a long time. It is my problem. Thanks heshan.lk and all. I am fix it. I use relax on frivjogo.info

Rob_Feature’s picture

Issue summary: View changes

Anyone looking to just print this directly in their node template can do this (or change the span to a div):

<span class="disqus-comment-count" data-disqus-identifier="node/<?php print $node->nid ?>"><!--Comment Counter Inserted Here --></span>
lslinnet’s picture

Do not hardcode the identifier, use the one in $node->disqus['identifier']; else you will have issues if you need to use another identifier.

7thkey’s picture

Does anyone know how to do it in Drupal 8? I need to show count comments on Teaser display.

renguer0’s picture

+1 to D8 request.

I will try that on localhost to do some testing.

ankithashetty’s picture

+1 to D8 request.