php - 如何覆盖子主题中的 CSS 文件?

标签 php css wordpress

有一个主题有各种 CSS 文件,我从那个主题创建了一个子主题。

然后我创建了一个新页面并添加了来自 functions.php 的 css 文件:

function my_theme_enqueue_styles() {
    if ( is_page_template( 'new-page.php' ) ) {
        wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'page-template', get_stylesheet_directory_uri() . '/css/page-style.min.css');
    }
}

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

因此,要为特定元素覆盖主题中的其他 CSS 文件,我必须使用 !important每次我想覆盖它们。

喜欢:

selector{
    property1: value !important;
    property2: value !important;
    ..
}
..

那么有没有办法在 </head> 之前包含该文件?例如什么的?

最佳答案

add_action('wp_enqueue_scripts', 'my_theme_enqueue_styles', PHP_INT_MAX);
    function my_theme_enqueue_styles(){

        wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/css/page-style.min.css', array('parent-style') );
    }

我在 function.php 中使用这段代码会起作用。通过这段代码,child theme 可以访问所有的parent theme css 并且可以添加新的 css 和覆盖父主题的 css。并且优先考虑child theme css。所以写 !important 是没有必要的。

关于php - 如何覆盖子主题中的 CSS 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50178532/

相关文章:

php - 在被入侵的 Wordpress 网站后删除缓存

php - 覆盖 WooCommerce CSS 问题

php - 如何使用 body_class 将 CSS 类添加到 Wordpress 中的 body 标签

PHP 超链接/从 SQL DB 中获取单个记录

css - Dark Liferay Theme - 定制合金弹窗

html - 声明文章的高度

javascript - 如何在 jQuery 中定位特定链接/点击?

php - PHP中变量范围的问题

php - 如何在 Symfony 4 中创建通用存储库

php - 谷歌 API 客户端 :"{ "错误“: "invalid_grant", "error_description": "Incorrect token type."}”