最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Facebook Comment Count

programmeradmin1浏览0评论

I am a bit of an amateur when it comes to PHP and WordPress but I have rigged up the following code:

function fb_comment_count($url = 'some url here') {
$filecontent = file_get_contents('/?ids=' . $url);
$json = json_decode($filecontent);
$count = $json->$url->comments;
if ($count == 0 || !isset($count)) {
    $count = 0;
}
echo $count;
}

What it does is it retrieves the comment count from the Facebook Graph and displays it on a page. For it to work, I have to manually declare the url for each call.

What I'm having a hard time with, is setting it up so that when you call the function in the template, it defaults to the post's permalink. I honestly have tried everything that has come to my mind.

get_permalink() doesn't work when you declare the arguments in the function.

Any help would be GREATLY appreciated. Thanks!

I am a bit of an amateur when it comes to PHP and WordPress but I have rigged up the following code:

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

What it does is it retrieves the comment count from the Facebook Graph and displays it on a page. For it to work, I have to manually declare the url for each call.

What I'm having a hard time with, is setting it up so that when you call the function in the template, it defaults to the post's permalink. I honestly have tried everything that has come to my mind.

get_permalink() doesn't work when you declare the arguments in the function.

Any help would be GREATLY appreciated. Thanks!

Share Improve this question asked May 29, 2011 at 4:45 Zack AustinZack Austin 1212 silver badges5 bronze badges 1
  • Finally got it to work the way i wanted it to. Turns out you shouldn't use file_get_contents() in WordPress. I used wp_remote_retrieve_body(wp_remote_get($url, array('sslverify'=>false))) – Zack Austin Commented May 30, 2011 at 17:54
Add a comment  | 

3 Answers 3

Reset to default 5

Final version of code used:

    function fb_comment_count($link = 'link') {
global $post;
$url = 'https://graph.facebook/';
$posturl = get_permalink($post->ID);
$url .= $posturl;

$filecontent = wp_remote_retrieve_body(wp_remote_get($url, array('sslverify'=>false)));
$json = json_decode($filecontent);
$count = $json->comments;
if ($count == 0 || !isset($count)) {
    $count = 0;
}

$comments = $count;
if ($count == 1) {
    $comments .= ' Comment';
}
elseif ($count == 0) {
    $comments = 'Leave a Comment';
}
elseif ($count > 1) {
    $comments .= ' Comments';
}
if ($link == 'nolink') {
    echo $comments;
}
else {
    echo '<a href="'.$posturl.'#comments" title="Comments for '.$post->post_title.'">'.$comments.'</a>';
}
}

Try this and see what it gives you:

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

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

Seems extremely complicated when you could just use this:

<fb:comments-count href="<?php echo get_permalink($post->ID); ?>"></fb:comments-count> Comments

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论