2013-04-26 69 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.- değil <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.

cevap

31

Update 11/3/2014:

We now have a method for using comment counts on any element you want. The regular count.js senaryo şimdi çalışacaktır eğer:

  • kullanın disqus-comment-count sınıf VE
  • kullanın Yani bir data-disqus-url VEYA data-disqus-identifier nitelik

şimdi ya bu unsurlar çalışacak:

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

ve

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

Eski Cevap o onun arayan etiketlerin tiplerine gelince count.js komut oldukça sert olduğunu

(artık bunu yapmayın) (o olmalı Bir a etiketi), bu yüzden bunu gerçekleştirmek için API'yı kullanmanız gerekir. http://disqus.com/api/docs/threads/set/

yüzünden API sınırlarını sen ideal olarak bu sunucuyu çalıştırmak gerekir:

Bu API çağrısı belirttiğiniz parçacığı herhangi bir sayı için iplik veri dizisini (siz "Mesajlar" tamsayı aradığınız) döndürür istemcilere hizmet vermek için saymak ve saymak. Ancak, çok yoğun bir siteniz yoksa, istemci tarafı yapmak genelde iyi olur. Uygulamanız için 1000'den fazla istek/saate ihtiyacınız varsa [email protected] adresine e-posta gönderebilirsiniz.

DÜZENLEME

Burada jQuery ile bunu nasıl hızlı bir örnek.

<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

Alkış Ryan Bu şuna benzer birçok boş div 's olduğunu varsayar. Bunun neden bir A etiketi olması gerektiğine dair çok tuhaf buluyorum - bana büyük bir göz kulak gibi geliyor. Sitemiz bugün yaklaşık 25-30 bin ziyaretçiyi alıyor, bu nedenle evet çok yoğun bir trafiğe sahip. Bu da, saat başına 1000+ talepte bulunabiliyor mu? Dün 73 bin sayfa görüntülendi. – pixelkicks

+1

Bize e-posta gönderin: [email protected] - Sınırı kaldırabiliriz, sadece her şeyin sorunsuz çalıştığından emin olmak için temas kurduğumuzdan emin oluruz :-) –

+0

Merhaba Ryan, e-posta göndermeyi denedik ama bir geri dönüş aldık iznimiz yok mu, yoksa Google grubu yok mu? – pixelkicks

0

Hayır jQuery Çözüm:

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);}); 
İlgili konular