2012-04-27 40 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>