简体   繁体   English

WordPress子主题中错误的入队脚本顺序

[英]wrong enqueue script order in wordpress child theme

I'm trying to enqueue my script in a child theme in wordpress, but i get it loaded before Jquery even though I've specified Jquery dependency. 我试图将脚本放入wordpress的子主题中,但是即使我已指定Jquery依赖关系,也要在Jquery之前加载它。

The code: 编码:

add_action( 'wp_enqueue_scripts', 'child_theme_scripts' );
function child_theme_scripts(){    
    wp_enqueue_script( 'dynamic-menu-script', get_stylesheet_directory_uri() . '/js/dynamic-menu.js', array( 'jquery' ) );
}

no matter what I do (I tried registering first, then specifying my script in the array after jquery, i tried to specify to load the script in the footer, i tried to enqueue JQuery externally) my script loads before query throwing me a "ReferenceError: Can't find variable: JQuery" I've checked JQ is loading properly but after my script. 无论我做什么(我先尝试注册,然后在jquery之后在数组中指定我的脚本,我试图指定在页脚中加载脚本,我试图在外部将JQuery入队),我的脚本在查询之前被加载,并抛出“ ReferenceError” :找不到变量:JQuery”我检查了JQ是否正确加载,但是在脚本之后。

this thing is driving me crazy, please help me... 这件事使我发疯,请帮助我...

You can improve the order of scripts loading with priority parameter. 您可以使用priority参数改善脚本加载的顺序。 Default priority is 10, so you may set it to 11. 默认优先级为10,因此您可以将其设置为11。

add_action( 'wp_enqueue_scripts', 'child_theme_scripts', 11 );

Here's a simple helper function that shows you what functions are assigned to the hook you want to use. 这是一个简单的帮助器函数,它向您显示要将哪些函数分配给要使用的钩子。

function print_filters_for( $hook = '' ) {
  global $wp_filter;
  if( empty( $hook ) || !isset( $wp_filter[$hook] ) )
    return;

  print '<pre>';
  print_r( $wp_filter[$hook] );
  print '</pre>';
}

you can call it anywhere in templates with print_filters_for( 'wp_enqueue_scripts' ); 您可以使用print_filters_for( 'wp_enqueue_scripts' );在模板中的任何位置调用它print_filters_for( 'wp_enqueue_scripts' );

In addition to @sirBlond, 除了@sirBlond,
In my case the hook = '' did not work. 在我的情况下, hook = ''无效。

I changed it to: 我将其更改为:

function print_filters_for($hook)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM