【问题标题】:Add Jquery from standard style in wordpress custom theme在 wordpress 自定义主题中从标准样式添加 Jquery
【发布时间】:2017-10-27 10:22:20
【问题描述】:

我在自定义主题的首页使用了以下代码,但想改进如何加载 jquery。请给我一些很酷的答案

/assets/js/jquery.min.js'> /assets/js/bootstrap.js'> /assets/js/jquery.mixitup.js'> /assets/js/custom-scripts.js'>

【问题讨论】:

    标签: jquery wordpress wordpress-theming


    【解决方案1】:

    请检查 WP 方式以包含脚本

    function ax_enqueue_style() {
    
        wp_enqueue_style( 'custom', get_template_directory_uri().'/custom.css' );
    } 
    
    add_action( 'wp_enqueue_scripts', 'ax_enqueue_style' );
    

    【讨论】:

      【解决方案2】:

      加载 jQuery 的最佳做法是在将自定义脚本加入队列时将其声明为依赖项。这样,如果插件使用 jQuery,您将不会再次加载它。假设您所有的插件也都遵循最佳实践。

      在你的情况下,它看起来像:

      //functions.php
      function my_theme_scripts(){
        wp_enqueue_script( 'bootstrap', get_template_directory_uri().'/assets/js/bootstrap.js', array('jquery'), '0', true );
        wp_enqueue_script( 'jquery-mixitup', get_template_directory_uri().'/assets/js/jquery.mixitup.js', array('jquery'), '0', true );
        wp_enqueue_script( 'custom-scripts', get_template_directory_uri().'/assets/js/custom-scripts', array('jquery'), '0', true );
      }
      add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );
      

      Wordpress 识别句柄“jquery”并以正确的顺序加载最新的稳定版本。

      注意:“0”是版本号,可以是您选择的任何数字。在“0.1”之前随时更新它,它将强制用户的浏览器重新下载缓存文件。

      参考资料: https://developer.wordpress.org/reference/functions/wp_enqueue_script/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多