Javascript 如何在wordpress中更改head中脚本的顺序?
我需要jquery.js首先在头部,然后是colorbox.js,然后是我的custom-script.js。我的自定义脚本依赖于jquery和colorbox。但是它在页面的标题中总是比colorbox.js高。我需要改变顺序。我该怎么做才能做到这一点?对不起我的英语Javascript 如何在wordpress中更改head中脚本的顺序?,javascript,wordpress,Javascript,Wordpress,我需要jquery.js首先在头部,然后是colorbox.js,然后是我的custom-script.js。我的自定义脚本依赖于jquery和colorbox。但是它在页面的标题中总是比colorbox.js高。我需要改变顺序。我该怎么做才能做到这一点?对不起我的英语 <?php /*Functions file for child theme*/ define( 'OPTIONS_FRAMEWORK_DIRECTORY', get_stylesheet_directory_uri(
<?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 );
?>
您应该将脚本包含在
wp\u enqueue\u脚本中,并将jQuery添加为依赖项,这样wordpress就可以处理顺序了
<?php
wp_enqueue_script( $handle, $src, $dependency, $ver, $in_footer );
?>
如何使用wp\u enqueue\u脚本调用脚本?
?我已经将我的functions.php的最新版本放在这里了。我不确定如何以正确的方式定义脚本和colorbox之间的依赖关系。Colorbox由插件php文件中的另一个插件嵌入。现在,我的脚本从页眉消失了。若我按照您所描述的那样做,我的自定义脚本将从页眉消失。你知道为什么吗?那是因为我把最后一个参数设置为true,这个参数清楚地标记为$in_footer,包括页面底部的脚本。只需将最后一个参数设置为false,脚本就会出现在头部。我尝试了false和true,但main.js从未出现在源代码中,如果存在数组('jquery','jquery.colorbox min'),则只有在没有类似于数组('jquery')的jquery.colorbox min'时才会出现我不知道我做错了什么数组应该包含其他脚本的处理程序,而不是脚本文件本身,请参见上面的示例,其中数组包含内置于wordpress的jquery
,然后只包含colorbox
,它是上面脚本的处理程序。
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' );