简体   繁体   English

AddThis Wordpress插件不适用于AJAX主题

[英]AddThis Wordpress plugin not working with AJAX theme

I'm using the AddThis Wordpress plugin to include share buttons underneath each post in an AJAX theme, and am having trouble getting the code right. 我正在使用AddThis Wordpress插件在AJAX主题的每个帖子下方添加共享按钮,并且在正确编写代码时遇到了麻烦。

I inserted the following code in the custom button field on the settings page: 我在设置页面的自定义按钮字段中插入了以下代码:

<div class="addthis_toolbox addthis_default_style" id="toolbox">
    <a class="addthis_button_preferred_1"></a>
    <a class="addthis_button_preferred_2"></a>
    <a class="addthis_button_preferred_3"></a>
    <a class="addthis_button_preferred_4"></a>
    <a class="addthis_button_compact"></a>
</div>

Then in the post loop in the main page PHP file I have this: 然后在主页PHP文件的发布循环中,我得到了以下内容:

<script>
    var tbx = document.getElementById("toolbox"),
    svcs = {email: 'Email', print: 'Print', facebook: 'Facebook', expanded: 'More'};

    for (var s in svcs) {
        tbx.innerHTML += '<a class="addthis_button_'+s+'">'+svcs[s]+'</a>';
    });

    function ReinitializeAddThis() {
        if (window.addthis) {
            window.addthis.toolbox("#toolbox");
        }
    }
</script>
<iframe style="display: none;" onload="ReinitializeAddThis();"></iframe>

The buttons will only load sporadically, and disappear after a refresh. 这些按钮只会偶尔加载,刷新后消失。 I'm also getting the error Uncaught ReferenceError: ReinitializeAddThis is not defined which is confusing... I'd really appreciate any insight and I apologize if anything is unclear. 我还收到错误Uncaught ReferenceError:ReinitializeAddThis尚未定义 ,这令人困惑...我非常感谢任何见解,如果有任何不清楚之处,我深表歉意。 Thanks! 谢谢!

I have arrived at a solution, so I'll post it here in case anyone ever runs into the same problem. 我已经找到了解决方案,因此,如果有人遇到相同的问题,我将在此处发布。

On the AddThis settings page, I pasted this code into the "custom button" field: 在AddThis设置页面上,我将此代码粘贴到“自定义按钮”字段中:

<div class="addthis_toolbox addthis_default_style" id="toolbox">
    <a class="addthis_button_preferred_1"></a>
    <a class="addthis_button_preferred_2"></a>
    <a class="addthis_button_preferred_3"></a>
    <a class="addthis_button_preferred_4"></a>
    <a class="addthis_button_compact"></a>
</div>

Then in the post loop within the main page PHP file: 然后在主页PHP文件的post循环中:

<script type="text/javascript">
        $(document).ajaxComplete(function(){
            console.log("All AJAX requests completed");

            if (window.addthis) {
                window.addthis.toolbox(".addthis_toolbox addthis_default_style");
                console.log("AddThis rendered")

            };
        });
        var tbx = document.getElementById("toolbox"),
            svcs = {email: 'Email', print: 'Print', facebook: 'Facebook', expanded: 'More'};

        for (var s in svcs) {
            tbx.innerHTML += '<a class="addthis_button_'+s+'">'+svcs[s]+'</a>';
        };
    </script>

This calls the window.addthis.toolbox() function for each individual post and creates a sharing toolbox after AJAX has rendered as specified on this page . 这将为每个单独的帖子调用window.addthis.toolbox()函数,并在按此页面上的指定呈现AJAX之后创建一个共享工具箱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM