个性化阅读
专注于IT技术分析

从WordPress自定义主题中的标准样式添加Jquery

点击下载

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

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


#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/

赞(0)
未经允许不得转载:srcmini » 从WordPress自定义主题中的标准样式添加Jquery

评论 抢沙发

评论前必须登录!