首页 > 解决方案 > 如何使用本地 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

标签: javascripthtmlwordpress

解决方案


在你的 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 );
}; }; ?>

希望这就是你要找的


推荐阅读