css - WordPress wp_enqueue_scripts 不工作

标签 css wordpress

我有一个主题和一个子主题,我正在尝试加载子主题的空白 css 文件以覆盖父主题文件规则。

在父主题中,主题根目录下有style.css,/assets/css/目录下有responsive.css

子主题子根目录下有style.css和responsive.css

functions.php 中有这段代码,但唯一有效的是如果我应用 !important 到子样式 css 规则。

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );

function theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/assets/css/responsive.css' );
    wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/responsive.css' );

如何让我的自定义文件在主 css 文件之后加载?

最佳答案

您可以通过声明依赖关系来确定加载顺序。请注意,所有句柄必须是唯一的:

add_action( 'wp_enqueue_scripts', 'so27575579_enqueue_styles' );
function so27575579_enqueue_styles()
{
    wp_register_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    wp_register_style( 'parent-style-resp', get_template_directory_uri() . '/assets/css/responsive.css', array( 'parent-style' ) );
    wp_register_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( 'parent-style-resp' ) );
    wp_register_style( 'child-style-resp', get_stylesheet_directory_uri() . '/responsive.css', array( 'child-style' ) );

    wp_enqueue_style( 'child-style-resp' );
}

关于css - WordPress wp_enqueue_scripts 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27575579/

相关文章:

javascript - 如何在 Materialise Framework Parallax 中使图像变大

html - 像表格一样显示嵌套列表

wordpress 插件或任何允许我们编辑每个帖子的元标记的东西?

html - 首次访问时未加载自定义字体

css - 如何使中间框中的内容垂直居中?

css - css 选择器 nth-child 如何应用于数组?

javascript - Bootstrap 4 card white space 添加responsive col类后,将元素包装在div内还是直接添加类?

php - 使用 get_object_vars () 后从 select 语句获取变量;

wordpress - Azure Linux 启动命令被忽略

php - 如何将多个 PHP 变量传递给 jQuery 函数?