How can I change order of scripts in head in wordp

2019-09-06 07:30发布

I need jquery.js would be first in the head then colorbox.js then my custom-script.js. My custom script depends on jquery and colorbox. But it always higher in the head of page then colorbox.js. I need to change that order. What should I do to do this? Sorry for my english.

<?php 

/*Functions file for child theme*/
define( 'OPTIONS_FRAMEWORK_DIRECTORY', get_stylesheet_directory_uri() . '/inc/' );
require_once dirname( __FILE__ ) . '/inc/options-framework.php';
function mychildtheme_setup() {
    show_admin_bar(false);
    wp_register_script( 'main', get_stylesheet_directory_uri() . '/js/main.js', array('jquery',     'jquery.colorbox-min'), false, false);
    wp_enqueue_script( 'main' );
}

add_action('after_setup_theme', 'mychildtheme_setup');
add_action( 'wp_enqueue_scripts', 'mychildtheme_setup', 10000 );
?>

1条回答
祖国的老花朵
2楼-- · 2019-09-06 08:11

You should include the script with wp_enqueue_script and add jQuery as a dependency, that way wordpress takes care of the order

<?php 
    wp_enqueue_script( $handle, $src, $dependency, $ver, $in_footer ); 
?>

so something like :

wp_register_script( 'colorbox', plugins_url('/path/colorbox.js', __FILE__), array( 'jquery' ), false, true );
wp_enqueue_script( 'colorbox' );

wp_register_script( 'custom', plugins_url('/path/custom-script.js', __FILE__), array( 'jquery', 'colorbox' ), false, true );
wp_enqueue_script( 'custom' );
查看更多
登录 后发表回答