2013-04-26 17 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.Mostrando Disqus commento conteggio in un DIV o SPAN - non <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.

risposta

31

Update 11/3/2014:

We now have a method for using comment counts on any element you want. The regular count.js script ora funzionerà se:

  • Utilizzare un disqus-comment-count di classe e
  • Utilizzare un data-disqus-url O data-disqus-identifier attributo

Così ora uno dei questi elementi avrebbero lavorato:

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

e

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

Old Risposta (non farlo più)

Lo script count.js è abbastanza inflessibile quando si tratta di tipi di tag sua ricerca di (deve essere un tag a), quindi è necessario utilizzare l'API per realizzare questo.

Questa chiamata API restituisce un array di dati di thread (cerchi il numero intero "Messaggi") per qualsiasi numero di thread che si specifica: http://disqus.com/api/docs/threads/set/

a causa dei limiti di API si incorrerà idealmente questo server -side e memorizza i conteggi nella cache per servire ai client. Tuttavia, a meno che tu non abbia un sito molto occupato, farlo sul lato client di solito va bene. Puoi inviare un'email a [email protected] se hai bisogno di più di 1000 richieste/ora per la tua applicazione.

EDIT

Ecco un rapido esempio di come si possa fare questo con jQuery. Ciò presuppone che si dispone di più div vuoti di che assomigliano a questo:

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

le 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

Acclamazioni Ryan. Trovo molto strano il motivo per cui deve essere un tag A - mi sembra una grande svista. Il nostro sito riceve circa 25-30k visitatori oggi, quindi sì, è molto alto traffico - che potrebbe metterlo nella categoria 1000+ richieste per ora? Aveva 73k pagine viste ieri. – pixelkicks

+1

Inviaci un'email a [email protected] - possiamo superare il limite, vogliamo solo assicurarci di aver contattato per assicurarci che tutto funzioni senza problemi :-) –

+0

Ciao Ryan, abbiamo appena provato l'email ma abbiamo ottenuto un ritorno dicendo che non abbiamo l'autorizzazione o che il gruppo Google non esiste? – pixelkicks

0

No jQuery Soluzione:

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);}); 
Problemi correlati