一页上有几个disqus-threads

Esp*_*tad 31 javascript global-variables disqus

我们有一个网站,我们列出了很多活动,并希望为每个活动添加讨论.

所以我们想使用disqus,并检查出来.原来他们使用全局变量来配置实例.

喜欢;

var disqus_shortname = '';

var disqus_identifier = '';

var disqus_url = '';
Run Code Online (Sandbox Code Playgroud)

当我们不想使用相同的标识符,而是每个disqus实例使用唯一的标识符时,这给我们带来了问题.尝试将每个实例化+配置放在iframe中,但这真的搞砸了ie8.有没有更好的方法呢?

所以,总结一下; 一页上的几个disqus实例.怎么样?还有其他人做过吗?

谢谢

小智 21

我们遇到了类似的问题,并通过电子邮件向Disqus发送了关 他们证实,默认情况下,他们每页仅支持一个Disqus模块.

在浏览Disqus JS文档时,我确实找到了一个可能适合您的解决方案,方法是在用户与站点交互时加载和卸载Disqus模块:

DISQUS.reset({
  reload: true,
  config: function () {  
    this.page.identifier = "newidentifier";  
    this.page.url = "http://example.com/#!newthread";
  }
});
Run Code Online (Sandbox Code Playgroud)

http://docs.disqus.com/help/85/

确切的实现取决于您的站点,但这应该为您提供一个构建块.例如,如果通过扩展内容区域可以获得事件信息,则只要有人展开事件内容,您就可以加载Disqus模块.


mys*_*dat 17

我写了一篇关于此的文章,在这里找到它.http://mystrd.at/articles/multiple-disqus-threads-on-one-page/

本质上,如果您一次只显示一个模块并使用某种"显示注释"控件,那么您可以通过以下方式执行此操作(使用Wordpress和jQuery作为示例,但您可以调整内容标识符根据您的需要而定).在post循环中,为每个插入一个额外的控件:

<a onclick="loadDisqus(jQuery(this), '<?= $id ?> <?= $post->guid ?>', '<? the_permalink() ?>');">
   Show comments
</a>
Run Code Online (Sandbox Code Playgroud)

之后,您需要一个通用函数,它将使用这些post参数并根据需要重新加载Disqus.请注意2012版本的Disqus还没有reset()方法,因此无法使用它.

// global vars used by disqus
var disqus_shortname = 'yoursiteshortnameindisqus';
var disqus_identifier; // made of post id &nbsp; guid
var disqus_url; // post permalink

function loadDisqus(source, identifier, url) {
    if (window.DISQUS) {
        jQuery('#disqus_thread').appendTo(source.parent()); // append the HTML to the control parent

        // if Disqus exists, call it's reset method with new parameters
        DISQUS.reset({
            reload: true,
            config: function() {
                this.page.identifier = identifier;
                this.page.url = url;
            }
        });
    } else {
        //insert a wrapper in HTML after the relevant "show comments" link
        jQuery('<div id="disqus_thread"></div>').insertAfter(source);
        disqus_identifier = identifier; // set the identifier argument
        disqus_url = url; // set the permalink argument

        // append the Disqus embed script to HTML
        var dsq = document.createElement('script');
        dsq.type = 'text/javascript';
        dsq.async = true;
        dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
        jQuery('head').appendChild(dsq);
    }
};
Run Code Online (Sandbox Code Playgroud)

除此之外,我相信你不得不求助于使用iframe.这里概述了以Ruby为例的这种解决方案 - http://blog.devinterface.com/2012/01/how-to-insert-more-disqus-box-in-single-page/

  • 感谢@mystrdat,受到你的回答的启发,我创建了[inlineDisqussions](http://tsi.github.io/inlineDisqussions/).我希望它会对任何人有所帮助. (3认同)