【问题标题】:I want to display a FB comment box on every page but have their own comments specific to that page我想在每个页面上显示一个 FB 评论框,但有针对该页面的自己的评论
【发布时间】:2012-07-14 04:23:20
【问题描述】:

我最近为我正在制作的网站编写了自己的博客。我想使用 facebook 来处理帖子的评论,所以我集成了插件。然而,我注意到了一些奇怪的事情。如果我对一篇博文发表评论,然后转到另一篇博文,我的评论也会在其他博文上发表。我如何使每个帖子都有自己的一组 cmets?

【问题讨论】:

    标签: facebook plugins comments facebook-javascript-sdk facebook-social-plugins


    【解决方案1】:

    您必须为每个帖子设置不同的 data-href 属性。以下是我在 Yii 中的操作示例:

    <div class="fb-comments" data-href="http://domain.com/post/view/<? echo $model->id; ?>" data-num-posts="4" data-width="695"></div>
    

    关键是每个帖子的 data-href 值必须是唯一的。

    【讨论】:

      【解决方案2】:

      假设您使用的是 html5 cmets 框。

      参考:https://developers.facebook.com/docs/reference/plugins/comments/

      data-href="" // data href 为盒子设置 url “object id”,你需要使用带有 php、javascript 或任何你编码的语言的动态系统。


      <div style="padding-left:5px; min-height:500px" class="fb-comments" data-href="'+newUrl+'" data-num-posts="20" data-width="380"></div>
      

      “我使用”示例: 以下示例适用于具有动态 url 的页面

      example.com?id=thisid&thisarticle=article-name
      

      <div id="thecomments"></div>
          <script>
          function changeCommentsUrl(newUrl){
          // should refresh fb comments plugin for the "newUrl" variable
          document.getElementById('thecomments').innerHTML='';
          parser=document.getElementById('thecomments');
          parser.innerHTML='<div style="padding-left:5px; min-height:500px" class="fb-comments" data-href="'+newUrl+'" data-num-posts="20" data-width="380"></div>';
          FB.XFBML.parse(parser);
          }
          var thisurl = document.write(document.URL);
          changeCommentsUrl(thisurl);
          </script>
      

      【讨论】:

        【解决方案3】:

        你可以简单地将这种 sn-p 放在你的页脚中:

        <script>
            $(".fb-comments").attr("data-href", window.location.href);
        </script>
        

        它完成了工作......

        【讨论】:

        • +1!好把戏。尽管请记住附加 data-href 值必须在 调用 facebook 脚本之前完成。只是指出,因为在文档中,当没有 data-href 值时,它是在 标记之后加载的。
        猜你喜欢
        • 2020-07-28
        • 1970-01-01
        • 2015-04-13
        • 2012-02-10
        • 2012-06-30
        • 1970-01-01
        • 2013-01-13
        • 2011-12-12
        • 1970-01-01
        相关资源
        最近更新 更多