在WooCommerce中强制重新加载style.css

问题描述 投票:0回答:2

我想强制用户重新加载style.css,因为他们的浏览器缓存可能会使设计混乱。我正在尝试使用它的版本(例如style.css?ver = 2)。

这是最好的方法吗?最终,我可能会考虑像link所示运行此动态。但是现在我什至无法使它正常工作。

我正在使用Salient Theme,如果我是正确的话,他们有一个enqueue.php脚本可以加载.css文件。现在,我已经将?ver = 2添加到下面的代码中。但是,当我查看主页时,这仍然会更改输出。我已经清除了缓存,并仔细检查了是否允许查询字符串。但是服务器缓存和WP-rocket似乎允许这样做。任何有关如何使此功能强制style.css重新加载的指示信息?

if( is_child_theme() ) {
        $nectar_theme_version = nectar_get_theme_version();
        wp_register_style( 'salient-child-style', get_stylesheet_directory_uri() . '/style.css?ver=2', '', $nectar_theme_version );
        wp_enqueue_style( 'salient-child-style' );
    }

UDPATE:我尝试将代码更改为/style2.css。那似乎确实更新了输出。看来我的设置正确,但查询已被切断。

html css wordpress stylesheet
2个回答
0
投票

将这些元标记放入header.php文件中(在head标记内部:

<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE">

0
投票

我将使用filemtime以编程方式对其进行版本控制。即使版本未显示,浏览器应该仍在读取新文件:

if( is_child_theme() ) {
        $nectar_theme_version = nectar_get_theme_version();
        wp_enqueue_style( 
            'salient-child-style', 
            get_stylesheet_directory_uri() . '/style.css', 
            [], 
            filemtime( get_stylesheet_directory() . '/style.css' ), 
            'all' 
        );
    }

基本上,这将获得文件的修改时间,如果文件的修改时间不同,则将更新版本,并让浏览器知道版本已更改。另外,您确实不需要单独的wp_register_style

© www.soinside.com 2019 - 2024. All rights reserved.