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

 
Khi nào cần chọn Quick Format, khi nào cần chọn Full Format?

Khi nào cần chọn Quick Format, khi nào cần chọn Full Format?

Trên máy tính sử dụng hệ điều hành Windows, có hai lựa chọn để định dạng bộ nhớ có thể gây cho bạn nhầm lẫn Quick Format và Full Format. Vậy khi nào thì nên chọn Quick Format và khi nào nên chọn Full Format?

ổ mSATA SSD dung lượng cao được Mushkin công bố

ổ mSATA SSD dung lượng cao được Mushkin công bố

Mushkin vừa công bố ra thị trường ổ mSATA SSD có dung lượng lưu trữ cao được thiết kế hướng đến các hệ thống ultrabook cao cấp và máy tính nhỏ gọn trong tương lai.

Giới thiệu thủ thuật thuyết trình bằng Powerpoint 2015

Microsoft Powerpoint tích hợp trong bộ Office là một phần mềm thông dụng nhất hiện nay 

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

 

Diet con trung