我有一個Wordpress網站,並有一個.js文件,我想鏈接,所以我可以調用腳本。經過大量研究,我發現這必須在functions.php中掛鉤。我嘗試了各種各樣的變化,但無法獲得正式的方法,但我找到了一種行之有效的方法,但只是在此過程中打破了網站的其他部分。正確的方式來鉤Wordpress中的jQuery腳本
我在做什麼錯?我知道get_stylesheet_directory_uri()
& get_template_directory_uri()
與父母和孩子主題的差異,但似乎沒有任何區別。
這裏是「官方」的方式不起作用:
function add_jquery_script() {
wp_enqueue_script(
'my-script', // name your script so that you can attach other scripts and de-register, etc.
get_stylesheet_directory_uri() . 'http://<site>.com/wp-content/themes/dt-the7/custom-scripts.js', // this is the location of your script file
array('jquery') // this array lists the scripts upon which your script depends
);
}
的「不建議在所有」的作品,但會導致網站的其他方式:一如既往
function add_jquery_script() {
echo '<script type="text/javascript" src="http://<site>/wp-content/themes/dt-the7/custom-scripts.js"></script>' . "\n";
}
add_action('wp_head', 'add_jquery_script');
,非常感謝任何幫助。謝謝你們
UPDATE
呼應了get_stylesheet_directory_uri()
我可以看到我的URL必須是相對的,現在應該是如下之後,但它仍然是行不通的。
function add_jquery_script() {
wp_enqueue_script(
'my-script', // name your script so that you can attach other scripts and de-register, etc.
get_stylesheet_directory_uri() . '/custom-scripts.js', // this is the location of your script file
array('jquery') // this array lists the scripts upon which your script depends
);
}
當你說「不行」你什麼意思,其標記一個PHP/JavaScript的/頭錯誤,確實腳本鏈接輸出,但不會叫,是輸出錯誤的,錯的地方,什麼? – Edward 2014-09-22 15:16:18
@愛德華它什麼都不做。就好像腳本沒有被調用一樣。我剛剛意識到我的網址應該在get_stylesheet_directory_uri()'回聲後相對,但即使現在它仍然不起作用。謝謝 – Ryan 2014-09-22 15:18:03
好 - 如此失敗,你有錯誤報告嗎? 'error_reporting(E_ALL);' – Edward 2014-09-22 15:19:20