为什么wordpress找不到我的javascript文件?为什么我的 javascript 文件没有刷新? [英] Why is my javascript file not found by wordpress? and why doesn't my javascript file refresh?

查看:36
本文介绍了为什么wordpress找不到我的javascript文件?为什么我的 javascript 文件没有刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 wp_enqueue_script() 向 wordpress 主题添加了自定义 javascript功能.

I added custom javascript to a wordpress theme through the wp_enqueue_script() function.

我将我的文件放在 theme-folder/js/my-javascript.js

但它不会被wordpress找到!我会在 wordpress 插入的 html 源代码中看到脚本标记,但单击它会将我带到未找到页面".我四处看看这是否是一些缓存问题.关闭缓存,还是什么都没有.

But it would not be found by wordpress! I would see the script tag in the html source inserted by wordpress, but clicking on it would take me to a "not found page". I looked around everywhere to see if this was some caching issue. Turned off caching, still nothing.

最后,我最终将 my-javascript.js 文件放在主题文件夹的根目录中,而不是 'js' 文件夹.现在我的 js 文件正在被找到并加载.

Finally, I ended up placing my-javascript.js file in the root of theme-folder, instead of 'js' folder. And now my js file was being found and loading.

但是现在出现了一个我不明白的新问题..我会编辑我的文件,但更改不会反映出来!每次我进行更改时,我都必须更改我的 js 文件的文件名,还要在我的入队脚本函数中重命名文件...我只是想完成这项任务.

But now a new problem I don't understand.. I would edit my file and the changes wouldn't reflect! I would have to change the file name of my js file everytime I made a change, renaming the file in my enqueue script function as well...I just wanted to get this task done.

那么为什么会发生这种情况呢?该网站托管在dreamhost上,我怀疑他们使用了一键安装程序.Dreamhost 是否会自行进行某种缓存?这就是为什么?

So why does this happen? The site is hosted on dreamhost, and I suspect they used a one-click installer. Does dreamhost do some sort of caching on its own? and that's why?

我傻吗?还是 wordpress 以某种方式知道我讨厌它?

Am I dumb? or does wordpress somehow know that I hate it?

这就是我加载脚本的方式以及我如何让它继续工作,但是为什么当我进行更改时我的脚本不会刷新,为什么当我将它放在主题的js"文件夹中时它不起作用?

This is how I'm loading the script and how I left it working, but why won't my script refresh when I make changes and why doesn't it work when I place it in theme's 'js' folder?

/* Add floater script */
function add_floater_div_script(){  
    /* wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);  */
    if ( is_page(1208) ) {  
       wp_enqueue_script( 'custom-floater', get_template_directory_uri() . '/custom-floater-8.js', array('jquery'), '', true);  
    }             
}  
add_action( 'wp_enqueue_scripts', 'add_floater_div_script' ); 

将代码从array ("更改为array(",因为有人认为这是一个问题.我自己快速检查了一下,PHP 对这个空间没问题,所以这不解释我试图解决的奇怪的 wordpress 行为.

Changed code from "array ( " to "array(" because someone thought this was a problem. I did a quick check myself and PHP is ok with this space so this does not explain the weird wordpress behaviour I'm trying to solve.

推荐答案

是的,缓存真的很烦人,虽然硬重新加载应该可以解决问题,但我们不能指望客户每次都这样做.WordPress 已经添加了一种方法来解决这个问题:)

Yeah, caching gets really annoying, Though hard reloading should solve the prob but we can't expect client to do it everytime. And WordPress has added a way to take care of that :)

wp_enqueue_script 接受第四个参数作为版本号...

wp_enqueue_script accepts 4th parameter as version number...

https://developer.wordpress.org/reference/functions/wp_enqueue_script/

所以每次更新后更新版本...

So just update version after each update...

对于开发,只需将其设置为 time()... 这样它每次都会重新加载并且从不缓存...

For development, Just set it as time()... So that it is reloaded every single time and never cached...

wp_enqueue_script( 'custom-floater', get_template_directory_uri() . '/custom-floater-8.js', array('jquery'), time(), true);

你的代码看起来像这样...

Your code will look something like this...

/* Add floater script */
function add_floater_div_script(){  
    /* wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);  */
    if ( is_page(1208) ) {  
       wp_enqueue_script( 'custom-floater', get_template_directory_uri() . '/custom-floater-8.js', array('jquery'), time(), true);
    }             
}  
add_action( 'wp_enqueue_scripts', 'add_floater_div_script' ); 

务必从生产版本中删除 time().或者你的文件永远不会被浏览器缓存...开发完成后设置你的主题版本,这样每个新版本都会更新JS.

BE SURE TO REMOVE time() FROM PRODUCTION VERSION. Or your file will never be cached by browser... Just set the version of your theme after dev is complete, so that every new version gets JS updated.

这篇关于为什么wordpress找不到我的javascript文件?为什么我的 javascript 文件没有刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆