【问题标题】:CSS JS not updating in wordpressCSS JS 没有在 wordpress 中更新
【发布时间】:2021-06-07 15:31:26
【问题描述】:

我正在使用 WordPress 并正在开发一个子主题。我在子主题的资产文件夹中添加了一个 custom.css 文件。并在 assets 文件夹中添加了一个文件夹 js,并添加了一个脚本文件 custom_script.js。第一次进行测试时,我在 css 中添加了警报和一些正文样式。运行良好,但一段时间后,我从这些文件中删除了所有内容,但没有更新,也没有添加新内容。请检查是什么问题。提前致谢。网址是https://mealsgroupuk.com/

这里是子主题function.php

<?php
// // Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exi
 // BEGIN ENQUEUE PARENT ACTION
 // AUTO GENERATED - Do not modify or remove comment markers above or below:

if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
    wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array( 'iconmoon','bootstrap','bootstrap-theme','chosen','swiper' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 1 );

// END ENQUEUE PARENT ACTION



/* Enqueues the external CSS file */
add_action( 'wp_enqueue_scripts', 'custom_external_styles' );
function custom_external_styles() {

wp_register_style( 'custom-css', get_stylesheet_directory_uri().'/assets/custom.css' );
wp_enqueue_style( 'custom-css' );

}

/* Enqueues the external js file */
function my_scripts_method() {
wp_enqueue_script(
    'custom-script',
    get_stylesheet_directory_uri() . '/assets/js/custom_script.js',
    array( 'jquery' )
);
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

子主题> 资产 > custom.css

child-theme> assets > js > custom_script.js

现在这两个文件都是空白的,但显示的是旧内容。

【问题讨论】:

标签: javascript php html css wordpress


【解决方案1】:

可能是版本缓存问题(入队)。 您可以更改版本或简单地使用 time() 每次获取最新版本。

wp_enqueue_script(
   'custom-script',
    get_stylesheet_directory_uri() . '/assets/js/custom_script.js',
    array('jquery'),
    time()
);

不要忘记在生产中替换 time()(使用主题或插件版本)以获得浏览器和服务器缓存的好处。

【讨论】:

  • 警告!将 time() 用于脚本版本不是一个好习惯,因为您丢失了浏览器缓存功能。更好的解决方案是使用 filemtime 获取给定文件的最新修改时间
  • @ZecKa 仅用于开发目的。你应该知道的。
  • BTW 对于开发,您还可以禁用浏览器上的缓存!我认为最好编辑答案以提及仅用于开发目的。
猜你喜欢
  • 1970-01-01
  • 2014-08-21
  • 2018-04-29
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 2019-03-11
  • 2015-07-08
相关资源
最近更新 更多