2012-04-27 73 views
0

我succeded通過這個功能來獲取Facebok的評論數:的WordPress + Facebook的評論除了(PHP所需要的知識)

<?php 
function fb_comment_count() { 
global $post; 
$url = get_permalink($post->ID); 

$filecontent = file_get_contents('https://graph.facebook.com/?ids=' . $url); 
$json = json_decode($filecontent); 
$count = $json->$url->comments; 
if ($count == 0 || !isset($count)) { 
    $count = 0; 
} 
echo $count; 
} 
;?> 

而且我把它叫做:

<?php fb_comment_count();?> 

現在我該怎樣添加它到此代碼:

<?php comments_number(__('No Comments'), __('1 Comment'), __('% Comments'), '', __('Comments Closed')); ?> 

這樣WordPress就可以在一個數字中顯示WP和FB註釋的數量。

非常感謝大家!

回答

2

將fb_comment_count函數的最後一行更改爲return $count;。這樣你就可以有一個號碼來處理。

現在內循環,補充一點:

<?php 
$fb_comments = fb_comment_count(); 
$wp_comments = get_comments_number(); 
$total_comments = $fb_comments + $wp_comments; 
printf ("<p>There are %d Total Comments: %d Wordpress Comments + %d Facebook Comments</p>\n", 
     $total_comments, $wp_comments, $fb_comments); 
?> 
+0

哇,你是偉大的,這就像一個魅力,你只是忘記$ fb_comments – OriginalEXE 2012-04-27 14:55:12

+0

前面非常感謝你,我在債務:) – OriginalEXE 2012-04-27 14:55:25

0

你也可以得到你的文章/頁面的評論在這個片段中

<fb:comments-count href="<?php the_permalink(); ?>"></fb:comments-count></div>