[英]include javascript files in wordpress theme using functions.php file
i have theses javascript files -> 我有这些javascript文件->
bootstrap.min.js;
jquery-1.10.1.min.js
modernizr-2.6.2-respond-1.1.0.min.js
function wpt_register_js() {
wp_register_script( 'jquery.bootstrap.min',
get_template_directory_uri() . '/js/bootstrap.min.js',
'jquery' );
wp_enqueue_script('jquery.bootstrap.min');
}
and now to add other scripts do i need make a new function for each of the scripts or can i use the same function for all of them 现在要添加其他脚本,我是否需要为每个脚本创建一个新功能,或者我可以对所有脚本使用相同的功能吗?
You can try something like this in your functions.php 您可以在您的functions.php中尝试这样的事情
add_action( 'wp_enqueue_scripts', 'add_in_queue_and_register_custom_scripts' );
function add_in_queue_and_register_custom_scripts(){
wp_register_script( 'my-script', get_stylesheet_directory_uri() . '/js/custom-script.js' );
wp_enqueue_script( 'custom-script', get_stylesheet_directory_uri() . '/js/custom-script.js', array( 'jquery', 'my-script' ) );
}
The bootstrap file can go in the footer. 引导文件可以放在页脚中。 Modernizr has to go in the header. Modernizr必须放在标题中。
To include these JS file do the following: 要包含这些JS文件,请执行以下操作:
function wpse_enqueue_scripts() {
wp_enqueue_script( 'modernizr', get_template_directory_uri() . '/js/modernizr-2.6.2-respond-1.1.0.min.js', array(), '2.6.2' );
wp_enqueue_script( 'jquery-bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array( 'jquery' ), false, true );
}
add_action( 'wp_enqueue_scripts', 'wpse_enqueue_scripts' );
You don't need to enqueue jQuery. 您不需要加入jQuery。 By defining it as a dependency for the second script it will load automatically. 通过将其定义为第二个脚本的依赖关系,它将自动加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.