gpt4 book ai didi

WordPress 根据 Facebook 评论显示热门帖子

转载 作者:行者123 更新时间:2023-12-02 22:33:58 24 4
gpt4 key购买 nike

所以我的想法是根据 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>

谢谢!

最佳答案

您可能希望将评论数量存储到 post meta-data这样您稍后就可以使用它进行排序。

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

SELECT commentsbox_count FROM link_stat WHERE url = 'POST_URL'

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

  • 在后元中存储评论数量
  • 查看帖子后调用 fb_comment_count 更新评论数量
  • 使用query_posts使用 meta_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 meta您可以在The Loop中使用query_posts :

query_posts('posts_per_page=5&meta_key=facebook_comments_count&orderby=meta_value&order=DESC')

关于WordPress 根据 Facebook 评论显示热门帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8919791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com