Thủ thuật đếm total Number cho Disqus Comments trong Page in Drupal 7

Thủ thuật đếm total Number cho Disqus Comments trong Page in Drupal 7

On a recent project I had to show the total number of comments posted using Disqus for each node on a page. What the page did was loop through a bunch of nodes and render the teaser view of each node.  The teaser was shown by rendering node.tpl.php, and in that file I had to display the total number of comments. Clicking a teaser view took me to the full page of the node that also contained the Disqus commenting box.

Thủ thuật đếm total Number cho Disqus Comments trong Page in Drupal 7

To do this I had to do add code in two places:

First, I added the following code in my theme's template.php hook_preprocess_html. The code puts the required Disqus javascript snippet in the footer.  I replaced SHORTNAME with the short name I chose when I was configuring Disqus for my site:

function mytheme_preprocess_html(&$variables, $hook) {
 $disqus_js = "
  /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
  var disqus_shortname = 'SHORTNAME'; // 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_js, 
  array(
   'type' => 'inline',
   'scope' => 'footer'
  )
 );
}

Second, I added the following <a> tag in node.tpl.php.  What's important is the hash #disqus_thread. The javascript above will look for links formatted this way and insert "5 comments" in the inner HTML of the tag.

<a href="<?php print url(drupal_get_path_alias('node/' . $node->nid));?>#disqus_thread"></a>

Technically the total comments can be shown in any template file as long as the Disqus javascript snippet and <a> tags are on the page.

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Hướng dẫn flag , unflag trong Drupal 7

Hướng dẫn flag , unflag trong Drupal 7

Drupal's Flag module comes in handy in various situations. It does however have one major drawback for some use cases - there is no way to clear all flags of a certain content item.

Running Drupal in Kubernetes with Docker in production

Running Drupal trong Kubernetes với Docker in production

You might think that Drupal and Docker work together nicely. They definitely can and do, in many cases, as we see with local development environments built around Docker

Facebook giới thiệu đơn vị quảng cáo di động để phát triển ứng dụng

Facebook giới thiệu đơn vị quảng cáo di động để phát triển ứng dụng

Chỉ vài tuần sau khi giới thiệu đơn vị quảng cáo di động đầu tiên, Facebook đã vận hành một đơn vị quảng cáo khác được thiết kế để giúp các nhà phát triển ứng dụng di động tự quảng bá cho mình.

Công ty diệt chuột T&C

 

Diet con trung