2013-04-26 48 views
12

We currently have our Disqus comment counts showing on each post on our homepage inside an <a href> tag, and we see this is updated by some javascript which detects whether #disqus_thread is present on the link.顯示Disqus留言數在DIV或SPAN - 不<a href>

How do we show the comment count outside of an tag though?

Is this possible?

We're not interested in having a direct link to the comments, so we'd like to remove the link and just show the count alone.

回答

31

Update 11/3/2014:

We now have a method for using comment counts on any element you want. The regular count.js腳本將現在的工作,如果你:

  • 使用disqus-comment-count
  • 使用data-disqus-urldata-disqus-identifier屬性

所以現在任這些元素將工作:

<span class="disqus-comment-count" data-disqus-url="http://example.com/path-to-thread/"> <!-- Count will be inserted here --> </span>

<span class="disqus-comment-count" data-disqus-identifier="your_disqus_identifier"> <!-- Count will be inserted here --> </span>

老答案(不要再這樣做了)

的count.js腳本是相當固定的,當涉及到的各類標籤的尋找(它必須是一個a標籤),所以你需要使用API​​來完成這個。

這個API調用返回線程數據數組(你正在尋找的「帖子」整數)爲您指定的任何數量的線程:http://disqus.com/api/docs/threads/set/

由於API限制你最好運行該服務器並緩存計數以供客戶端使用。但是,除非你有一個非常繁忙的網站,否則在客戶端做這件事通常很好。如果您的應用程序需要超過1000個請求/小時,則可以發送郵件至[email protected]

編輯

這裏是你如何能做到這一點使用jQuery一個簡單的例子。這是假設你有幾個空的div的看起來像這樣:

<div class="my-class" data-disqus-url="http://example.com/some-url-that-matches-disqus_url/"></div> 

文件的javascript:

$(document).ready(function() { 

     var disqusPublicKey = "YOUR_PUBLIC_KEY"; 
     var disqusShortname = "YOUR_SHORTNAME"; 
     var urlArray = []; 

     $('.my-class').each(function() { 
      var url = $(this).attr('data-disqus-url'); 
      urlArray.push('link:' + url); 
     }); 


     $('#some-button').click(function() { 
      $.ajax({ 
       type: 'GET', 
       url: "https://disqus.com/api/3.0/threads/set.jsonp", 
       data: { api_key: disqusPublicKey, forum : disqusShortname, thread : urlArray }, // URL method 
       cache: false, 
       dataType: 'jsonp', 
       success: function (result) { 

        for (var i in result.response) { 

         var countText = " comments"; 
         var count = result.response[i].posts; 

         if (count == 1) 
          countText = " comment"; 

         $('div[data-disqus-url="' + result.response[i].link + '"]').html('<h4>' + count + countText + '</h4>'); 

        } 
       } 
     }); 

}); 
+0

乾杯瑞安。我覺得很奇怪,爲什麼它必須是A標籤 - 對我來說好像是一個很大的疏漏。我們的網站今天收到大約25-30k的訪問者,所以是非常高的流量 - 這可能會使其每小時1000+個請求類別?它昨天有73k頁面瀏覽量。 – pixelkicks 2013-05-03 07:08:34

+1

發郵件給[email protected] - 我們可以達到極限,我們只是想確保我們已經聯繫,以確保一切順利:-) – 2013-05-05 04:47:49

+0

嗨,瑞安,我們剛剛嘗試過電子郵件,但我們得到了反彈說我們沒有權限或Google組不存在? – pixelkicks 2013-05-10 13:42:25

0

沒有jQuery的解決方案:

var gettingCount = false; 
var countCallerCallback = null; 
function countCallback(data) // returns comment count or -1 if error 
{ 
    var count = -1; 
    try { 
     var thread = data.response[0]; 
     count = thread === undefined ? "0" : thread.posts; 
    } 
    catch (ex) { 
     console.log("FAILED TO PARSE COMMENT COUNT"); 
     console.log(ex); 
    } 

    // always do this part 
    var commentCountScript = document.getElementById("CommentCountScript"); 
    document.getElementsByTagName('head')[0].removeChild(commentCountScript); 
    countCallerCallback(count); 
    gettingCount = false; 
    countCallerCallback = null; // if this got reset in the line above this would break something 
} 
function getCommentCount(callback) { 
    if(gettingCount) { 
     return; 
    } 
    gettingCount = true; 

    var script = document.createElement('script'); 
    script.id = "CommentCountScript"; 
    var apiKey = "api_key=MY_COOL_API_KEY"; 
    var forum = "forum=MY_FORUM_SHORT_NAME" 
    var thread = "thread=" + "link:" + window.location.href; 
    script.src = 'https://disqus.com/api/3.0/threads/set.jsonp?callback=countCallback&' + apiKey + "&" + forum + "&" + thread; 
    countCallerCallback = callback; 
    document.getElementsByTagName('head')[0].appendChild(script); 
} 
getCommentCount(function(count){alert(count);}); 
相關問題