Search code examples
blogssquarespace

Facebook plugin on Squarespace blog


I added the Facebook plug-in in the post-code injection block, but then every new blog post includes the same “test” comment of mine. Any idea how to correct so that each new blog has a cleared FB comment section?enter image description here?


Solution

  • The code, as provided to you by Facebook, does not produce a dynamically changing URL for each new blog post. So, we must set the data-href attribute dynamically. The simplest way to do this is to use the following instead of what you're using now:

    <div class="fb-comments" data-href="" data-width="" data-numposts="5"></div>
    <script>
      document.getElementsByClassName("fb-comments")[0].setAttribute("data-href", window.location.hostname + window.location.pathname);
    </script>
    

    In the code above, we leave the data-href attribute empty, and then fill it in using JavaScript immediately following the div. The script then inserts the URL of the current page, making the comments unique to that page.