亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

您是否應該在 wp_register_script/style 和 wp_enqueue_

您是否應該在 wp_register_script/style 和 wp_enqueue_

PHP
一只甜甜圈 2023-07-15 15:47:46
我想知道在注冊腳本并指定依賴項后,在將其排隊時,您是否應該再次指定依賴項?按照這個好的邏輯,我會說不,但我不知道......你的燈很感激!實施例1add_action( 'wp_enqueue_scripts', 'theme_scripts' );function theme_scripts() {  if( ! is_admin() ) {    wp_register_script( 'script_one_js', '//path/to/script/one.js' );    wp_register_script( 'script_two_js', '//path/to/script/two.js', array( 'script_one_js' ) );    // Specifying dependency again    wp_enqueue_script( 'script_one_js' );    wp_enqueue_script( 'script_two_js', array( 'script_one_js' ) );  };};實施例2add_action( 'wp_enqueue_scripts', 'theme_scripts' );function theme_scripts() {  if( ! is_admin() ) {    wp_register_script( 'script_one_js', '//path/to/script/one.js' );    wp_register_script( 'script_two_js', '//path/to/script/two.js', array( 'script_one_js' ) );    // NOT specifying dependency again    wp_enqueue_script( 'script_one_js' );    wp_enqueue_script( 'script_two_js' );  };};
查看完整描述

1 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

不,您不需要兩次指定依賴項。


要了解該選項為何存在,您需要了解wp_enqueue_script和 的wp_register_script用途。


在將腳本放入隊列之前,您根本不必注冊腳本,您可以單獨使用wp_enqueue_script它,如果尚未注冊,它將注冊它 - 這就是為什么您在兩個腳本中都有相同的選項(包括設置依賴項)功能。


例如,您可以將其添加到您的functions.php廣告中,它將同時注冊和排隊腳本:


add_action( 'wp_enqueue_scripts', 'theme_scripts' );

function theme_scripts() {

    if( ! is_admin() ) {

        wp_enqueue_script( 'script_1_js', '//path/to/script/one.js' );

        wp_enqueue_script( 'script_2_js', '//path/to/script/two.js', array( 'script_1_js' ) );

    }

}

那為什么還要注冊腳本呢?有很多優點,例如:


您可以在一個位置使用正確的依賴項和其他設置注冊一次腳本,然后僅在需要它們的頁面或情況下加載它們。除了不必要地加載腳本之外,這也意味著您不必填寫在您使用該腳本的函數的每個地方的所有參數中wp_enqueue_script。

例如你可以在你的functions.php中包含這個:


add_action( 'wp_enqueue_scripts', 'theme_scripts' );

function theme_scripts() {

    if( ! is_admin() ) {

        wp_register_script( 'script_1_js', '//path/to/script/one.js' );

        wp_register_script( 'script_2_js', '//path/to/script/two.js', array( 'script_1_js' ), '1.1', true );

    }

}

現在,例如,如果您只需要加載script_two_js2 個特定的頁面模板,則可以添加wp_enqueue_script( 'script_1_js' );到這些模板中,而不必每次都添加依賴項、版本,因為它從注冊時就已經知道了這一點。


您還可以使用 注冊您的腳本wp_register_script,如果您有另一個腳本在其依賴項中包含此腳本,那么它將在該腳本之前自動加載,而無需將其排隊。

例如,如果您有 four.js,它依賴于其他 3 個腳本,例如


add_action( 'wp_enqueue_scripts', 'theme_scripts' );

function theme_scripts() {

    if( ! is_admin() ) {

        wp_register_script( 'script_1_js', '//path/to/script/one.js' );

        wp_register_script( 'script_2_js', '//path/to/script/two.js' );

        wp_register_script( 'script_3_js', '//path/to/script/three.js' );

        wp_register_script( 'script_4_js', '//path/to/script/four.js', array( 'script_1_js', 'script_2_js', 'script_3_js' );

    }

}

現在您可以使用 at 加載腳本,wp_enqueue_script( 'script_4_js' );它將自動加載腳本一、二和三。


希望這有助于回答您的問題!


查看完整回答
反對 回復 2023-07-15
  • 1 回答
  • 0 關注
  • 116 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號