如何向 WordPress 添加一个简单的 jQuery 脚本?
WordPress 是一个用于开发动态网站的开源 CMS。下面让我们学习如何向 WordPress 添加 jQuery 脚本。在 WordPress 主题 文件夹中,创建一个文件夹“js”,并添加一个文件。该文件将是你带有“.js”扩展名的查询文件。将 jQuery 脚本添加到该文件。我们已将其命名为 new.js,
这是你的 jQuery 脚本
jQuery(document).ready(function($) {
$('#nav a').last().addClass('last');
});打开你主题的 functions.php 文件。使用 wp_enqueue_script() 函数,以便你可以添加你的脚本。
此代码如下
add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
// name your script to attach other scripts and de-register, etc.
wp_enqueue_script (
‘new',
get_template_directory_uri() . '/js/add_my_script.js',
array('jquery') // this array lists the scripts upon which your script depends
);
}假设你的主题在正确的位置具有 wp_head 和 wp_footer ,这就能正常工作。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP