简体   繁体   English

如何在WordPress子主题中的functions.php上添加多个脚本?

[英]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.

相关问题 通过子主题函数向PHP插件自定义帖子类型添加选项 - Add options to a Wordpress plugin Custom Post Type via child theme functions.php 如何在Wordpress子主题functions.php中正确排队retina.js? - How do I enqueue retina.js in a Wordpress child theme functions.php properly? 使用wp_enqueue_script()在Wordpress-functions.php中添加Javascript - Add Javascript in Wordpress - functions.php with wp_enqueue_script() 如何在functions.php(wordpress)中加载bootstrap脚本和样式? - How to load bootstrap script and style in functions.php (wordpress)? Javascript和Wordpress:子主题的Functions.php,用于更改JS文件上的值 - Javascript and Wordpress: Functions.php for a child theme to change a value on a JS file 如何在 WordPress 函数.php 上添加 datalayer.push? - How to add datalayer.push on WordPress functions.php? 如何在函数中添加/关闭简码。WordPress 中的 php - How to add/close a shortcode to functions.php in WordPress 调试wordpress主题,而无需更新functions.php中的脚本和样式版本 - Debug wordpress theme without updating script and style versions in functions.php 使用functions.php文件在wordpress主题中包含javascript文件 - include javascript files in wordpress theme using functions.php file WordPress functions.php wp_enqueue_script无效 - WordPress functions.php wp_enqueue_script not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM