我想在我的wordpress网站上包含一个来自github的插件(https://github.com/rendro/countdown)。经过几个小时的研究,在这里,我仍然无法找到使其工作的方法。在Wordpress中包含一个github jquery插件
如果我使用的默认方法:
1)添加这些线到我header.php
文件的<head>
:
<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的这两次我增强了加载时间,以及,右)
这意味着入队的jQuery,然后我在我的functions.php
文件脚本像这样:
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, 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>
我目前在我functions.php
:
// Loads JavaScript file with functionality specific to LCQC.
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');
The jquery plugin (.js)
'head'中的脚本会使网站变慢。它最好在关闭主体标签之前使用脚本。这样所有的内容都被加载。当完成时,JavaScript被加载。这样用户已经看到了一些东西。 – SuperDJ
我的脚本标记位于我的''和'
'标记之间,而不在''之间。不过谢谢你的这个小建议。我会确保修改! –它不起作用?控制台中有任何错误? – vard