是否为WordPres中的模板添加多个wp_Register_脚本?

时间:2017-11-20 作者:Henry

如何将多个JavaScript文件添加到模板?

下面的内容只适用于加载到模板template-1上的一个Javascript文件。php和template-2。php-但我还有另外3个JavaScript文件。

  // Template ABC
  if ( is_page_template( array(\'template-1.php\',\'template-2.php\'))):
    wp_register_script( \'Name of JS File\', \'https://url.js\' );
    wp_enqueue_script(\'Name of JS File\');
  endif;
如果我想将其他Javascript文件添加到相同的模板中,下面的方法是否正确?

  // Template ABC
  if ( is_page_template( array(\'template-1.php\',\'template-2.php\'))):
    wp_register_script( \'Name of JS File\', \'https://url.js\' );
    wp_register_script( \'Name of NEW JS File 1\', \'https://url-new-1.js\' );
    wp_register_script( \'Name of NEW JS File 2\', \'https://url-new-2.js\' );
    wp_enqueue_script(\'Name of JS File\');
  endif;

1 个回复
最合适的回答,由SO网友:Johansson 整理而成

虽然将所有JS文件合并到一个JavaScript中并将其排队以减少HTTP请求是一种更好的做法,但也可以将它们单独排队。因此,您可以注册脚本(不是必需的),然后将其排队。

只需在注册所有脚本后将其排队,如:

  // Template ABC
  if ( is_page_template( array(\'template-1.php\',\'template-2.php\'))):

    wp_register_script( \'Name of JS File\', \'https://url.js\' );
    wp_register_script( \'Name of NEW JS File 1\', \'https://url-new-1.js\' );
    wp_register_script( \'Name of NEW JS File 2\', \'https://url-new-2.js\' );

    wp_enqueue_script(\'Name of JS File\');
    wp_enqueue_script(\'Name of JS File 1\');
    wp_enqueue_script(\'Name of JS File 2\');
  endif;
或直接将其全部排队:

  // Template ABC
  if ( is_page_template( array(\'template-1.php\',\'template-2.php\'))):

    wp_enqueue_script( \'Name of JS File\', \'https://url.js\' );
    wp_enqueue_script( \'Name of NEW JS File 1\', \'https://url-new-1.js\' );
    wp_enqueue_script( \'Name of NEW JS File 2\', \'https://url-new-2.js\' );

  endif;

结束

相关推荐