問題描述

在加載 style.css 之前如何排隊.css 文件?或者使默認 style.css 依賴於另一個.css 文件?

我正在嘗試加載.css 重置,哪個 style.css 將覆蓋。

這是我有的:

add_action('wp_enqueue_scripts', 'load_css_files');

function load_css_files() {
    wp_register_style( 'normalize', get_template_directory_uri() . '/css/normalize.css');
    wp_enqueue_style( 'normalize' );
}

但是這是在 style.css 之後加載的。

最佳解決方案

也排隊 style.css,並將 normalize 設置為依賴關係:

if ( ! is_admin() )
{
    // Register early, so no on else can reserve that handle
    add_action( 'wp_loaded', function()
    {
        wp_register_style(
            'normalize',
            // parent theme
            get_template_directory_uri() . '/css/normalize.css'
        );
        wp_register_style(
            'theme_name',
            // current theme, might be the child theme
            get_stylesheet_uri(), [ 'normalize' ]
        );
    });
    add_action( 'wp_enqueue_scripts', function()
    {
        wp_enqueue_style( 'theme_name' );
    });
}

當打印 theme_name 時,WordPress 將首先自動加載依賴項。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。