如何在wordpress中更改head中的脚本顺序? [英] How can I change order of scripts in head in wordpress?

查看:28
本文介绍了如何在wordpress中更改head中的脚本顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先需要 jquery.js,然后是 colorbox.js,然后是我的 custom-script.js.我的自定义脚本取决于 jquery 和 colorbox.但它总是在页面头部高于 colorbox.js.我需要改变那个顺序.我该怎么做才能做到这一点?对不起我的英语.

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 );
?>

推荐答案

你应该用 wp_enqueue_script 包含脚本,并添加 jQuery 作为依赖项,这样 wordpress 会处理顺序

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 ); 
?>

所以像:

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' );

这篇关于如何在wordpress中更改head中的脚本顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆