admin管理员组

文章数量:1301490

function load_my_script(){
wp_register_script( 'my_script', get_template_directory_uri() . '/js/myscript.js', 'jquery' );
wp_enqueue_script( 'my_script', 'jquery');
}
add_action('wp_enqueue_scripts', 'load_my_script');

myscript.js is loading before jquery, why? And how do I make it load AFTER jquery?

Thanks.

function load_my_script(){
wp_register_script( 'my_script', get_template_directory_uri() . '/js/myscript.js', 'jquery' );
wp_enqueue_script( 'my_script', 'jquery');
}
add_action('wp_enqueue_scripts', 'load_my_script');

myscript.js is loading before jquery, why? And how do I make it load AFTER jquery?

Thanks.

Share Improve this question asked Apr 10, 2012 at 17:22 DaveDave 8104 gold badges9 silver badges21 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 21

You have a typo in your code. It should be:

function load_my_script(){
    wp_register_script( 
        'my_script', 
        get_template_directory_uri() . '/js/myscript.js', 
        array( 'jquery' )
    );
    wp_enqueue_script( 'my_script' );
}
add_action('wp_enqueue_scripts', 'load_my_script');

The jQuery dependency needs to be an array(), not just a string. This will force your script to load after jQuery.

get_template_directory_uri() does give you the theme directory, but this is not what you want if you are using a Child Theme.

get_stylesheet_directory_uri() will give your the directory of your "current theme", so in either case, it is safest to use.

本文标签: How do I make script load after jquery