[英]How to add multiple script on functions.php in wordpress child theme?
I did try to put my scripts in functions.php but its not showing only the font awesome and all-script.js but the other script is not working. 我确实尝试将我的脚本放到functions.php中,但它不只显示真棒字体和all-script.js,而另一个脚本却不起作用。 I want to add 5 more script.
我想再添加5个脚本。 How do i do that.
我怎么做。 i think i made some mistake here.
我想我在这里犯了一些错误。 Im not a coder so i hope you understand guys.
我不是编码员,所以希望您能理解。 Thank you
谢谢
//enqueues our locally supplied font awesome stylesheet function enqueue_our_required_stylesheets(){ wp_enqueue_style('font-awesome', get_stylesheet_directory_uri() . '/assets/css/font-awesome.min.css'); wp_enqueue_script( 'script', get_stylesheet_directory_uri() . '/assets/js/all-script.js'); wp_enqueue_script( 'script', get_stylesheet_directory_uri() . '//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js'); wp_enqueue_script( 'script', get_stylesheet_directory_uri() . '/libs/jquery/scrpt-1.js'); wp_enqueue_script( 'script', get_stylesheet_directory_uri() . '/libs/jquery/scrpt-2.js'); wp_enqueue_script( 'script', get_stylesheet_directory_uri() . '/libs/jquery/scrpt-3.js'); wp_enqueue_script( 'script', get_stylesheet_directory_uri() . '/libs/jquery/scrpt-4.js'); wp_enqueue_script( 'script', get_stylesheet_directory_uri() . '/libs/jquery/scrpt-5.js'); } add_action('wp_enqueue_scripts','enqueue_our_required_stylesheets');
Try This Line of Code: 尝试以下代码行:
function enqueue_our_required_stylesheets(){
wp_enqueue_style('font-awesome', get_stylesheet_directory_uri() . '/assets/css/font-awesome.min.css');
wp_enqueue_script( 'script-all', get_stylesheet_directory_uri() . '/assets/js/all-script.js');
wp_dequeue_script( 'jquery' );
wp_enqueue_script( 'script-jquery', get_stylesheet_directory_uri() . '//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js');
wp_enqueue_script( 'script-1', get_stylesheet_directory_uri() . '/libs/jquery/scrpt-1.js');
wp_enqueue_script( 'script-2', get_stylesheet_directory_uri() . '/libs/jquery/scrpt-2.js');
wp_enqueue_script( 'script-3', get_stylesheet_directory_uri() . '/libs/jquery/scrpt-3.js');
wp_enqueue_script( 'script-4', get_stylesheet_directory_uri() . '/libs/jquery/scrpt-4.js');
wp_enqueue_script( 'script-5', get_stylesheet_directory_uri() . '/libs/jquery/scrpt-5.js');
}
add_action('wp_enqueue_scripts','enqueue_our_required_stylesheets');
You need to use a unique identifier for each script (first parameter for wp_enqueue_script
). 您需要为每个脚本使用唯一的标识符(
wp_enqueue_script
第一个参数)。 Since you've used script
in each case, WordPress ignores all but the first one, thinking it's already loaded and it wouldn't be wise to load it twice. 由于您在每种情况下都使用过
script
,因此WordPress会忽略除第一个script
所有script
,并认为它已被加载,并且两次加载都不明智。
Give them some meaningful names and you'll be able to use requirements (third parameter), eg tell wordpress that this script you're loading requires jQuery and OtherScriptOne. 给它们起一些有意义的名称,您就可以使用需求(第三个参数),例如,告诉wordpress您正在加载的此脚本需要jQuery和OtherScriptOne。 If those are registered (see Muhammed Hafil's answer), they will be loaded automatically and inserted before the script you're adding.
如果已注册(请参阅Muhammed Hafil的答案),它们将自动加载并插入到要添加的脚本之前。
you need to register script and style before using them and you should use different handle for each script and styles .correct format is 您需要先注册脚本和样式,然后再使用它们,并且每种脚本和样式都应使用不同的句柄。正确的格式是
function enqueue_our_required_stylesheets(){
wp_register_style('font-awesome', get_template_directory_uri() . '/assets/css/font-awesome.min.css');
wp_enqueue_style('font-awesome');
wp_register_script('script', get_template_directory_uri(). '/assets/js/all-script.js');
wp_enqueue_script('script' );
}
Note : you don't need to register jquery since it is registered by wordpress itself, you can just enqueue them directly without registering. 注意:您不需要注册jquery,因为它是由wordpress本身注册的,您可以直接将其加入队列而无需注册。 you can go through the wp registered scripts in their codex at https://developer.wordpress.org/reference/functions/wp_register_script/#core-registered-scripts
您可以在https://developer.wordpress.org/reference/functions/wp_register_script/#core-registered-scripts上查看其密码本中的wp注册脚本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.