gpt4 book ai didi

css - Wordpress - GeneratePress 主题 - 缓存破坏

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:10 25 4
gpt4 key购买 nike

我正在维护一个使用 GeneratePress 主题的 wordpress 网站。 为了扩展,使用了一个子主题解决方案。它已经是一个相当发达的 child 主题(这样说是为了避免戏剧性的解决方案)。

一段时间以来,每次更新 CSS 时,我都会遇到 CSS 缓存问题style.css 子样式表提供缓存版本而不是修改后的版本

我相信 GeneratePress 主题正在加载父 style.css + 子 style.css(如果存在)。

generatepress/functions.php

// Enqueue our CSS.
wp_enqueue_style( 'generate-style-grid', get_template_directory_uri() . "/css/unsemantic-grid{$suffix}.css", false, GENERATE_VERSION, 'all' );
wp_enqueue_style( 'generate-style', get_template_directory_uri() . '/style.css', array( 'generate-style-grid' ), GENERATE_VERSION, 'all' );
wp_enqueue_style( 'generate-mobile-style', get_template_directory_uri() . "/css/mobile{$suffix}.css", array( 'generate-style' ), GENERATE_VERSION, 'all' );
wp_add_inline_style( 'generate-style', generate_base_css() );

// Add the child theme CSS if child theme is active.
if ( is_child_theme() )
wp_enqueue_style( 'generate-child', get_stylesheet_uri(), true, filemtime( get_stylesheet_directory() . '/style.css' ), 'all' );

阅读这段代码...似乎如果检测到子主题,它应该已经清除了缓存 但是...对我来说它没有。 我没有得到一个带有 filemtime 结果作为版本参数的 url。

我的子主题 functions.php 没有排队任何“style.css”。尽管如此,它还是在父级 style.css 之后加载。

尝试 1我已经从 generatepress 阅读了添加 CSS 的部分 https://docs.generatepress.com/article/adding-css/ .

我不知道如何缓存子 style.css 文件。

尝试 2我第一次尝试解决此问题是基于在触发 wp_enqueue_scripts 时将样式表排入队列。

function theme_css(){
wp_enqueue_style( 'my-theme-style', get_stylesheet_directory_uri().'/style.css', 'generate-style', '1.1', 'all');
}
add_action( 'wp_enqueue_scripts', 'theme_css' );

我提到了 generate-style 因为在我看来它是父默认样式所依赖的(子 style.css 将在父 style.css 之后加载)。您可以在前面的代码片段中看到这一点。 它对我不起作用。子主题将首先加载,然后加载父主题样式表。 不幸的是,这会破坏 css。顺序应该相反(先是父 style.css,然后是子 style.css)。如前所述,依赖于generate-style 没有帮助。

尝试 3用另一个文件名创建样式表。 Generatepress 提示说,如果检测到 child_theme,它需要一个 style.css 样式表文件。我可以有一个带有空 style.css 和新样式表的子主题。问题是 style.css 缓存将为返回的用户提供。

我是 wordpress 的新手,所以我不知道它可能必须通过其他机制来解决这个问题。也许有一些更简单的解决方案可行?

最佳答案

如果您按照主题文档的建议使用 Autoptimize here ,那么您需要删除缓存,您的用户将看到更改。此引用来自该文档:

Installing Autoptimize

Reducing the amount of CSS and JS files being loaded on each page load will make a huge difference to your page speed.

Another perk with Autoptimize is it will bundle the CSS generated by your options in the Customizer into an external file, allowing your browser to cache it.

要删除和重置 Autoptimize 缓存:登录 wp-admin 并转到 Plugins/Autoptimize/Settings 以删除缓存,它会执行此操作并为您启动一个新的缓存。 wp-admin 的顶部菜单栏中还有一个 Autoptimize 项;这也很方便。

关于css - Wordpress - GeneratePress 主题 - 缓存破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46538578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com