2 回答

TA貢獻1934條經驗 獲得超2個贊
夫婦的事情。
您可以將版本/修訂添加到排隊鉤子本身,因此您不必擔心將版本連接到文件名并意外排除“?”等字符。鉤子會為你做到這一點。
您缺少第二個函數的 add_action 。
您應該將兩個 if 語句組合到同一個函數中。
嘗試這樣的事情:
<?php
function load_js_assets() {
// Declare your revision variables once
$datetime = new DateTime('now');
$revision = $datetime->format("YmdHis");
// Test against first statement. If true enqueue the first script
if( is_page( 'Example Page 1' ) ) {
wp_enqueue_script('example1.js', 'http://website.com/example1.js', array('jquery'), $revision, false);
}
// Test against second statement. If true enqueue the second script
if( is_page( 'Example Page 2' ) ) {
wp_enqueue_script('example2.js', 'http://website.com/example2.js', array('jquery'), $revision, false);
}
// Do more if needed...
}
add_action('wp_enqueue_scripts', 'load_js_assets');

TA貢獻1802條經驗 獲得超4個贊
您缺少第二個腳本add_action,這可能是第二個腳本無法正確加載的原因。試試這個代碼,它使用兩個函數,add_action如果您在您希望使用它們的頁面上:
function load_js_assets1() {
$datetime = new DateTime('now');
$revision = $datetime->format("YmdHis");
wp_enqueue_script('example1.js?'.$revision, 'http://website.com/example1.js', array('jquery'), '', false);
}
function load_js_assets2() {
$datetime = new DateTime('now');
$revision = $datetime->format("YmdHis");
wp_enqueue_script('example2.js'.$revision, 'http://website.com/example2.js', array('jquery'), '', false);
}
if( is_page( 'Example Page 1' ) ) {
add_action('wp_enqueue_scripts', 'load_js_assets1');
}
if( is_page( 'Example Page 2' ) ) {
add_action('wp_enqueue_scripts', 'load_js_assets2');
}
- 2 回答
- 0 關注
- 111 瀏覽
添加回答
舉報