php – 在WordPress中包含一个github jquery插件

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 在WordPress中包含一个github jquery插件脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在我的WordPress网站上以正确的方式包含来自 github( https://github.com/rendro/countdown)的插件.经过几个小时的研究,在堆栈上,我仍然无法找到一种方法使其工作.

如果我使用方法

1)将这些行添加到< head>我的header.PHP文件

<script tyPE="text/javascript" src="https://ajax.GOOGLEapis.COM/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="/path/to/jquery.countdown.js"></script>

2)在< / head>之间使用我想要的配置初始化插件和< body>在我的header.PHP文件中,在这种情况下:

<script>

    $(function() {
        VAR endDate = "November 30,2015 10:00:00";

            $('.countdown.styled').countdown({
                date: endDate,render: function(data) {
                $(this.el).htML("<div>" + this.leadingZeros(data.days,2) + " <span>days</span></div><div>" + this.leadingZeros(data.hours,2) + " <span>hours</span></div><div>" + this.leadingZeros(data.min,2) + " <span>;minutes</span></div><div>" + this.leadingZeros(data.sec,2) + " <span>seconds</span></div>");
                }
            });
    });

</script>

3)然后在我的html文件调用插件

<?PHP if(is_front_page() ) { ?>

    <div id="cdtoevent" class="countcont">
    <div class="countdown styled"></div>
    </div>

<?PHP } ?>

它完美无缺!

但是,如果我尝试以正确的方式做到一点

(如果我理解的话,wordpress认使用jQuery,通过使用方法我让用户下载jquery两次,这也增加了我的加载时间,对吗?)

这意味着在我的functions.PHP文件中将jquery和我的脚本排入队列,如下所示:

wp_enqueue_script('jquery');

function add_cdtoevent() {
        wp_enqueue_script('countdown',get_template_directory_uri() . 'countdown.js',array('jquery'),true );
}

add_action( 'wp_enqueue_scripts','add_cdtoevent' );

然后重复步骤2)和3)的方法,它根本不起作用!我尝试在步骤2)中包装我的jquery代码,就像这样:

jQuery(document).ready(function( $) {

    // $Works! You can test IT with next line if you like
    // console.LOG($);

});

但它仍然无效.
如果有人可以提供帮助,我们将非常感激!

我目前在header.PHP中有什么:

<?PHP if(is_front_page() ) { ?>

     <div id="cdtoevent" class="countcont">
     <div class="countdown styled"></div>
     </div>

<?PHP } ?>

我目前在footer.PHP中有什么:

<script type="text/javascript">

    $(function() {
        var endDate = "November 14,render: function(data) {
                        $(this.el).html("<div>" + this.leadingZeros(data.days,2) + " <span>seconds</span></div>");
            }
        });
    });

</script>

我目前在我的functions.PHP中有什么:

// Loads JavaScript file with functionality specific to LCQC.

wp_enqueue_script('jquery');

function add_cdtoevent() {
        wp_enqueue_script('countdown','add_cdtoevent' );

The jquery plugin(.js)

解决方法

使用函数将脚本排入队列是个好主意.但是:你在哪里调用这个功能?你需要调用它来排队你的脚本.执行此操作的最佳方法是将其附加到正确的操作(wp_enqueue_scripts):add_action(‘wp_enqueue_scripts’,’add_cdtoevent’);.

如果您想了解有关此功能的更多信息,我写了一篇关于including scripts in WordPress的指南.知道它是如何工作的很有用,因为你可以发现手动排队jQuery是没用的:你表明它是一个依赖项,所以wordpress会在需要时自动添加它.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 在WordPress中包含一个github jquery插件全部内容,希望文章能够帮你解决php – 在WordPress中包含一个github jquery插件所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。