3

所以我想到的是根据他们有多少 Facebook 评论显示一个列表,其中包含最受欢迎的帖子。我已经设法制作了一个函数来计算基于 facebook 图表的帖子有多少评论,但我遇到了查询问题:

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

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

<?php if ($count == 0) { ?>
         <span>No comment</span>
<?php } elseif ($count == 1) { ?>
         <span>One Comment</span>
<?php } elseif ($count > 1 ) { ?>
         <span><?php echo $count; ?> Comments</span>

谢谢!

4

2 回答 2

2

您可能希望存储评论数量以发布元数据,以便以后可以使用它进行排序。

顺便说一句,由于您使用的响应格式与实际响应不同,您的功能将无法正常工作。(评论的数量出现在response->comments->count而不是在response->comments)。此外,您可能希望fields=comments将响应限制为仅包含有关评论的详细信息而没有所有其余数据或使用 FQL 查询仅检索评论数:

SELECT commentsbox_count FROM link_stat WHERE url = 'POST_URL'

我看到的流程可能是这样的:

  • 在 post-meta 中存储评论数
  • fb_comment_count更新查看帖子后调用的评论数量
  • 使用query_postswithmeta_key更改默认值。
function fb_comment_count() {
  global $post;
  $url = get_permalink($post->ID);

  $query = "SELECT commentsbox_count FROM link_stat WHERE url = '{$url}'";
  $responseText = file_get_contents('https://graph.facebook.com/fql?q='.$query);
  $responseJson = json_decode($responseText);

  $commenteCount = $responseJson->data->commentsbox_count;
  update_post_meta($post->ID, 'facebook_comments_count, $commenteCount);
  // ...
}

一旦您的帖子具有facebook_comments_count 数据,您就可以query_postsThe Loop中使用:

query_posts('posts_per_page=5&meta_key=facebook_comments_count&orderby=meta_value&order=DESC')
于 2012-03-25T14:39:19.870 回答
1

您将想要访问 HTTP GET http://graph.facebook.com/comments?ids=,它将返回一个具有数据属性的对象。该数据属性将是一组评论对象(请参阅https://developers.facebook.com/docs/reference/api/Comment/

例如:

http://graph.facebook.com/comments?ids=http://www.stackoverflow.com/

{
   "http://www.stackoverflow.com/": {
      "data": [
         {
            "id": "450042939888_21515527",
            "from": {
               "name": "Anidhya Ahuja",
               "id": "1172382999"
            },
            "message": "abc",
            "created_time": "2011-10-11T13:55:15+0000"
         },
         {
            "id": "450042939888_21515536",
            "from": {
               "name": "Anidhya Ahuja",
               "id": "1172382999"
            },
            "message": "wass",
            "created_time": "2011-10-11T13:55:48+0000"
         }
      ],
      "paging": {
         "next": "http://graph.facebook.com/comments?ids=http\u00253A\u00252F\u00252Fwww.stackoverflow.com\u00252F&limit=25&offset=25&__after_id=450042939888_21515536"
      }
   }
}
于 2012-01-19T01:19:41.137 回答