档案中的WordPress Facebook评论计数


WordPress Facebook Comment Count in archive

我想在我的博客档案中显示Facebook评论数。

我的wordpress网站在function.php中有以下php函数

    // Get combined FB and WordPress comment count
    function full_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;
    $wpCount = get_comments_number();
    $realCount = $count + $wpCount;
    if ($realCount == 0 || !isset($realCount)) {
        $realCount = 0;
    }
    return $realCount;
    }

这就是我在循环<?php echo full_comment_count(); ?> 中的模板文件上使用函数的方式

大多数时候,大多数文章都显示"0"条评论。但有时其中一两个会起作用。我做错了什么

试试这个

function full_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;
}