我想在我的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