2014-09-22 25 views
2

我有一個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 
); 
} 
+0

當你說「不行」你什麼意思,其標記一個PHP/JavaScript的/頭錯誤,確實腳本鏈接輸出,但不會叫,是輸出錯誤的,錯的地方,什麼? – Edward 2014-09-22 15:16:18

+0

@愛德華它什麼都不做。就好像腳本沒有被調用一樣。我剛剛意識到我的網址應該在get_stylesheet_directory_uri()'回聲後相對,但即使現在它仍然不起作用。謝謝 – Ryan 2014-09-22 15:18:03

+0

好 - 如此失敗,你有錯誤報告嗎? 'error_reporting(E_ALL);' – Edward 2014-09-22 15:19:20

回答

2

PHP

你需要調用函數add_jquery_script();纔可以期待輸出任何東西 - 很簡單,很容易被遺忘。

JQuery的

劇本被打破,因爲一個引用錯誤的代碼,var price定義應該在函數內部移動,以便它在功能的scopehttp://learn.jquery.com/javascript-101/scope/

$(document).ready(function() { 
    refresh(); 
    $('#bittrex-price').load(bittrexPrice); 
}); 

function refresh() { 
    var price = "[domain hidden]/realtime/bittrex-realtime.php"; 
    setTimeout(function (price) { 
     $('#bittrex-price').fadeOut('slow').load(price, params).fadeIn('slow'); 
     $('#bittrex-vol').fadeOut('slow').load(price, params2).fadeIn('fast'); 
     refresh(); 
    }, 1000); 
} 
相關問題