Search code examples
javascriptphpmagentodisqus

Set up Disqus Multiple Canonical URL in Magento


I'm trying to integrate Disqus on my test Magento website. I'm using NeoTheme Blog Extension however I disabled the built in comment section and change it to disqus.

The problem was the canonical url. I have a main category http://?????.com/news-and-media/ that has a sub category /blog and /news and others. The blog page can be access as http://????/.com/news-and-media/blog-content and http://????/.com/news-and-media/blog/blog-content, both display the same content. I tried to comment on each url however the comment is missing from the other page and vice versa.

This is my progress so far.

My phtml

<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
this.page.url = 'http://?????.com/news-and-media/';
this.page.identifier = 'blog';
};
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://????-com.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

My local.xml

<neotheme_blog_post_index>
    <reference name="disquscomment">
        <block type="core/template" name="dcomment" template="disqus-comments.phtml" />
    </reference>        
</neotheme_blog_post_index>

Do you have solution for this canonical? I can't seemed to undesrtand the documentation and example.

https://help.disqus.com/customer/portal/articles/472098-javascript-configuration-variables

How to make Disqus to work with url routing?


Solution

  • You have to fetch the rel canonical value using javascript and use it as your page identifier. I'm not sure if this works.

    var canonical = "";
    var links = document.getElementsByTagName("link");
    for (var i = 0; i < links.length; i ++) {
    if (links[i].getAttribute("rel") === "canonical") {
        canonical = links[i].getAttribute("href")
    }
    }
    var disqus_config = function () {
    this.page.url = '';
    this.page.identifier = canonical;
    };
    (function() { // DON'T EDIT BELOW THIS LINE
    

    Check this link for reference. http://javascript.qahowto.com/Obtaining-canonical-url-using-JavaScript-javascript-url-5b420a