2013-04-26 6 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.Pokazuje Disqus komentarz liczyć w DIV lub SPAN - nie <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.

Odpowiedz

31

Update 11/3/2014:

We now have a method for using comment counts on any element you want. The regular count.js skrypt będzie teraz działać, jeśli:

  • Użyj disqus-comment-count klasa I
  • Użyj data-disqus-url LUB data-disqus-identifier atrybut

Więc teraz albo z te elementy będą pracować:

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

i

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

Old Odpowiedź (nie rób tego więcej)

Skrypt count.js jest dość sztywne, jeśli chodzi o rodzaje tagów jej szukają (musi być znacznik a), więc aby to osiągnąć, musisz użyć interfejsu API.

To wywołanie API zwraca tablicę danych wątek (szukasz do „Wiadomości” Integer) dla dowolnej liczby wątków, które należy podać: http://disqus.com/api/docs/threads/set/

powodu ograniczenia API będziesz idealnie uruchomić ten serwer -side i pamięć podręczną liczby, aby służyć klientom. Jednakże, chyba że masz bardzo obciążoną witrynę, robienie tego po stronie klienta jest zazwyczaj w porządku. Możesz wysłać e-mail do [email protected], jeśli potrzebujesz więcej niż 1000 zgłoszeń/godzinę dla swojej aplikacji.

EDIT

Oto krótki przykład tego, jak można to zrobić z jQuery. Zakłada się, że masz kilka pustych div że wyglądać tak:

<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

Cheers Ryan. Uważam, że to bardzo dziwne, dlaczego ma to być oznaczenie A - wydaje mi się dużym niedopatrzeniem. Nasza strona ma dziś ok. 25-30 tys. Odwiedzających, więc tak, jest to bardzo duży ruch - który może umieścić ją w ponad 1000 zgłoszeń na godzinę? Wczoraj było 73 tys. Wyświetleń strony. – pixelkicks

+1

Wyślij do nas e-mail na [email protected] - możemy zwiększyć limit, chcemy tylko upewnić się, że skontaktowaliśmy się, aby wszystko działało sprawnie :-) –

+0

Witaj, Ryan, właśnie próbowaliśmy wysłać e-mail, ale dostaliśmy bounceback mówiąc, że nie mamy pozwolenia lub grupa Google nie istnieje? – pixelkicks

0

Nie jQuery Rozwiązanie:

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);}); 
Powiązane problemy