javascript - 如何使用本地 Wordpress 环境为主题文件夹中的 JS 文件设置“src”路径
问题描述
试图将 JS 文件链接到我的标题以使其动态化。CSS 文件工作正常,因为我已将其链接到 functions.php 并从那里使用 @import 来选择文件夹中的 CSS 模块。最好这将是链接 JS 文件的最佳方式,但您可以链接多个吗?如果可以,如何链接?
目前我正在使用带有 url “js/header.js”的脚本标签,这会在开发工具中返回一个错误值
GET http://pd-g.local/.js/header.js net::ERR_ABORTED 404 (Not Found)
如何让 WP 找到 JavaScript 文件?
PD&G
┣ css
┃ ┗ header.css
┣ 图片
┃ ┗ 壁炉.jpg
┣ js
┃ ┗ header.js
┣ 页脚.php
┣ front-page.php
┣ 函数.php
┣ header.php
┣ index.php
┣ page.php
┣ post.php
┣ 截图.png
┗ style.css
解决方案
在你的 function.php 文件中:
<?php
//Add theme required js
add_action( 'wp_enqueue_scripts', 'theme_scripts' );
function theme_scripts() {
if( !is_admin() ) {
//Theme script
wp_enqueue_script(
'script_js',
get_template_directory_uri() . '/script.js',
array(), //Add script name to load after if needed
null,
true );
}; }; ?>
希望这就是你要找的
推荐阅读
- ios - 在 CoreData 中对关系进行排序
- sql - 如何自动化日期选择以反映历史时间段的窗口?
- geometry - 找到到一组给定点中任意点距离最短的新点
- c# - 背景图像未调整大小 - C#
- jquery - 指定列以仅在编辑时允许数字 vals(剑道网格)
- php - WooCommerce 中特定产品类别的十进制数量步骤
- python - 我是python新手,帮我理解一下,一行代码?
- reactjs - 在 react-router-dom 中传递参数链接不正常
- hyperledger-fabric - AWS 托管区块链捆绑库 NodeJS
- elf - 为什么 ELF 动态加载器加载的 DYNAMIC 段不在 ELF 可执行请求的地址?