2013-04-26 27 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.Hiển thị Disqus đếm bình luận trong một DIV hoặc SPAN - không <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.

Trả lời

31

Update 11/3/2014:

We now have a method for using comment counts on any element you want. The regular count.js kịch bản bây giờ sẽ làm việc nếu bạn:

  • Sử dụng một disqus-comment-count lớp VÀ
  • Sử dụng một data-disqus-url HOẶC data-disqus-identifier thuộc tính

Bây giờ một trong hai các yếu tố này sẽ hoạt động:

<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>

Old trả lời (không làm điều này nữa)

Kịch bản count.js là khá cứng nhắc khi nói đến các loại thẻ tìm kiếm của mình cho (nó phải là một thẻ a), vì vậy bạn sẽ cần phải sử dụng API để thực hiện việc này.

gọi API này trả về một mảng dữ liệu ren (bạn đang tìm kiếm "Bài đăng" số nguyên) cho bất kỳ số chủ đề mà bạn chỉ định: http://disqus.com/api/docs/threads/set/

Do giới hạn API tốt nhất bạn nên chạy máy chủ này -side và cache số lượng để phục vụ cho khách hàng. Tuy nhiên, trừ khi bạn có một trang web rất bận rộn, làm cho nó phía khách hàng thường là tốt. Bạn có thể gửi email [email protected] nếu bạn cần hơn 1000 yêu cầu/giờ cho đơn đăng ký của mình.

EDIT

Dưới đây là một ví dụ nhanh như thế nào bạn có thể làm điều này với jQuery. Điều này giả định rằng bạn có một vài nhân div trống trông như thế này:

<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. Tôi thấy nó rất lạ tại sao nó phải là một thẻ A - có vẻ như là một sự giám sát lớn đối với tôi. Trang web của chúng tôi nhận được khoảng 25-30 nghìn khách truy cập ngày hôm nay vì vậy có, lưu lượng truy cập rất cao - có thể đưa nó vào danh mục 1000 yêu cầu mỗi giờ? Nó đã có 73k lượt xem trang hôm qua. – pixelkicks

+1

Email cho chúng tôi tại [email protected] - chúng tôi có thể tăng giới hạn, chúng tôi chỉ muốn đảm bảo rằng chúng tôi đã liên hệ để đảm bảo mọi thứ hoạt động suôn sẻ :-) –

+0

Hi Ryan, chúng tôi vừa thử gửi email nhưng chúng tôi đã nhận được phản hồi nói rằng chúng tôi không có quyền hoặc nhóm Google không tồn tại? – pixelkicks

0

Không jQuery Giải pháp:

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);}); 
Các vấn đề liên quan