【问题标题】:<fb:comments-count> not working on my WordPress powered blog<fb:comments-count> 在我的 WordPress 驱动的博客上不起作用
【发布时间】:2011-08-22 19:48:17
【问题描述】:

我在 WordPress 上使用 Facebook cmets 插件,并且 cmets 框工作正常,但我想访问索引页和单页上的计数。在页面上,页面上加载了 Facebook Javascript。

这是我使用的代码: &lt;fb:comments-count href=&lt;?php echo get_permalink() ?&gt;/&gt;&lt;/fb:comments-count&gt; comments

但它不计算 FB cmets。

是否有一个简单的代码可以让我检索评论计数?

谢谢,

【问题讨论】:

  • 已解决。我使用

标签: facebook comments wordpress facebook-fql


【解决方案1】:

只需将此函数放在 functions.php 中,然后将帖子 url 传递给 function fb_comment_count,无论您在主题文件中如何调用它

function fb_comment_count($url) {
$filecontent    = file_get_contents('https://graph.facebook.com/comments/?ids=' . $url);
$json           = json_decode($filecontent);
$content        = $json->$url;

echo count($content->comments->data);

}

【讨论】:

    【解决方案2】:

    已解决。

    <p><span class="cmt"><fb:comments-count href=<?php the_permalink(); ?>></fb:comments-count></span> Comments</p>
    

    问题是我使用的是“url”而不是“href”属性。

    【讨论】:

      【解决方案3】:

      cmets 通常不会出现在这里:

      graph.facebook.com/?ids = [your url]
      

      相反,它们在

      中表现得很好
      graph.facebook.com/comments/?ids = [your url]
      

      因此最终解决方案的价值。

      【讨论】:

        【解决方案4】:

        这对我有用:

        function fb_comment_count() {
          global $post;
          $url = get_permalink($post->ID);
          $filecontent = file_get_contents('https://graph.facebook.com/comments/?ids=' . $url);
          $json = json_decode($filecontent);
          echo(count($json->$url->comments->data));
        }
        

        【讨论】:

          【解决方案5】:

          ifennec 的回答看起来不错,但实际上不起作用(facebook 可能改变了一些东西,现在只返回分享的数量)。

          你可以尝试获取所有的 cmets:

              $filecontent = file_get_contents(
                  'https://graph.facebook.com/comments/?ids=' . $url);
          

          并计算所有:

              $json = json_decode($filecontent);
              $content = $json->$url;
              $count = count($content->data);
          
              if (!isset($count) || $count == 0) {
                 $count = 0;
              }
              echo $count;
          

          这只是一个修复,直到 facebook 决定阅读有关 fb:cmets-count 的常见问题解答并发现它不起作用:) (http://developers.facebook.com/docs/reference/plugins/cmets/ 是的,很棒的 cmets)。

          顺便说一句,我在 Drupal 7 中应用了这个功能 :) 非常感谢 ifennec,你给我指路了。

          【讨论】:

            【解决方案6】:

            在你的模板文件中包含这个函数:

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

            在您的主页或任何地方像这样使用它

            <a href="<?php the_permalink() ?>"><?php fb_comment_count() ?></a>
            

            遇到同样的问题,该功能对我有用...如果您遇到错误...尝试阅读this

            【讨论】:

            • 代码有效,但在没有 cmets 时会抛出一些警告。通过将最后几行(“$json = ...”之后)替换为:return empty($json-&gt;$url-&gt;comments) ? 0 : (int) $json-&gt;$url-&gt;comments;
            猜你喜欢
            • 2018-12-29
            • 1970-01-01
            • 1970-01-01
            • 2015-12-19
            • 1970-01-01
            • 2015-07-02
            • 1970-01-01
            • 1970-01-01
            • 2015-09-13
            相关资源
            最近更新 更多