萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

WordPress 特定頁面載入專屬 CSS 樣式表:條件式樣式載入實作指南

WordPress 特定頁面載入專屬 CSS 樣式表:條件式樣式載入實作指南

在 WordPress 主題開發中,針對特定頁面(分類、文章類型、單頁、作者頁等)載入專屬 CSS,能避免全站載入不必要的樣式代碼,提升效能並隔離樣式作用域。本文詳細說明在 header.php 中實作條件式載入的方法、WordPress 條件標籤對照表、以及現代化替代方案。

核心實作:在 header.php 中條件判斷載入

基本架構

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?php wp_head(); ?>
    
    <!-- 條件式 CSS 載入區塊 -->
    <?php if (is_category('news')): ?>
        <link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/css/category-news.css">
    <?php elseif (is_page('about')): ?>
        <link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/css/page-about.css">
    <?php else: ?>
        <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>">
    <?php endif; ?>
</head>

WordPress 常用條件標籤速查表

條件函式適用場景參數範例
is_home()部落格首頁(最新文章頁)無參數
is_front_page()網站首頁(靜態頁面設為首頁時)無參數
is_single()單篇文章頁is_single(123), is_single('slug'), is_single([1,2,3])
is_page()單一頁面is_page('about'), is_page(42), is_page(['about','contact'])
is_category()分類彙整頁is_category('news'), is_category(5), is_category(['news','events'])
is_tag()標籤彙整頁is_tag('featured')
is_tax()自訂分類法彙整頁is_tax('genre', 'jazz')
is_archive()所有彙整頁(分類、標籤、日期、作者)無參數
is_author()作者彙整頁is_author('john')
is_date()日期彙整頁無參數
is_search()搜尋結果頁無參數
is_404()404 錯誤頁無參數
is_singular()單一內容頁(文章、頁面、自訂文章類型)is_singular('product')
is_post_type_archive()自訂文章類型彙整頁is_post_type_archive('product')

進階模式:模組化條件載入函式

避免在 header.php 堆疊大量判斷邏輯,建議在 functions.php 集中管理:

<?php
// functions.php
function mytheme_conditional_styles() {
    // 基礎樣式表(全站載入)
    wp_enqueue_style('main-style', get_stylesheet_uri(), [], '1.0.0');
    
    // 條件式載入
    if (is_category('news')) {
        wp_enqueue_style('cat-news', get_template_directory_uri() . '/css/category-news.css', ['main-style'], '1.0.0');
    } elseif (is_page('about')) {
        wp_enqueue_style('page-about', get_template_directory_uri() . '/css/page-about.css', ['main-style'], '1.0.0');
    } elseif (is_post_type_archive('product')) {
        wp_enqueue_style('product-archive', get_template_directory_uri() . '/css/product-archive.css', ['main-style'], '1.0.0');
    }
    
    // 特定頁面追加樣式(不替換基礎樣式)
    if (is_page('contact')) {
        wp_enqueue_style('page-contact-extra', get_template_directory_uri() . '/css/contact-extra.css', ['main-style'], '1.0.0');
    }
}
add_action('wp_enqueue_scripts', 'mytheme_conditional_styles');
?>

wp_enqueue_style 參數詳解

wp_enqueue_style(
    $handle,           // 唯一識別名稱(不可重複)
    $src,              // 檔案路徑(建議用 get_template_directory_uri())
    $deps = [],        // 依賴的其他樣式表 handle 陣列
    $ver = false,      // 版本號(用於快取失效,false 為 WP 版本,null 為不輸出)
    $media = 'all'     // 媒體類型:all, screen, print, (max-width: 768px) 等
);

現代化替代方案:區塊主題與全站編輯 (FSE)

WordPress 5.9+ 引入區塊主題,樣式管理範式轉變:

  • theme.json:集中定義色彩、字體、間距、版面設定,自動產出 CSS 變數與區塊樣式
  • 區塊樣式變體:在編輯器中為區塊註冊樣式變體,無需手寫 CSS
  • 全域樣式面板:使用者可在自訂介面即時調整全站/區塊樣式
  • wp_enqueue_style 仍相容,但建議漸進遷移至 theme.json + 區塊樣式

效能優化建議

  1. 合併壓縮:使用建置工具或外掛合併多個條件式 CSS 為單一檔案
  2. 關鍵 CSS 內聯:首屏關鍵樣式內聯,條件式 CSS 非同步載入
  3. 避免過度碎片化:每頁額外載入 1-2 個 CSS 尚可,超過 5 個建議重構
  4. 利用瀏覽器快取:正確設定版本號與快取標頭

常見錯誤與除錯

<?php
// ❌ 錯誤:在 header.php 直接 echo link,繞過 wp_enqueue_style
// 導致:無法去重、無法管理依賴、無法被子主題覆蓋、快取外掛無法處理
if (is_page('contact')) {
    echo '<link rel="stylesheet" href="' . get_template_directory_uri() . '/css/contact.css">';
}

// ✅ 正確:使用 wp_enqueue_style
// 甚至可在 footer 載入非關鍵 CSS
function mytheme_footer_styles() {
    if (is_page('contact')) {
        wp_enqueue_style('contact-form', get_template_directory_uri() . '/css/contact-form.css', [], '1.0.0', 'print');
        // media="print" 會延遲載入,頁面可互動後再套用
    }
}
add_action('wp_enqueue_scripts', 'mytheme_footer_styles');
?>

總結

WordPress 條件式 CSS 載入的現代最佳實踐:棄用 header.php 直寫 link,改用 functions.php + wp_enqueue_style 集中管理。善用 WordPress 豐富的條件標籤,配合依賴管理與版本控制,建立清晰、可維護、高效能的樣式載入架構。對於區塊主題專案,應優先考慮 theme.json 與區塊樣式系統,減少手寫 CSS 需求。

copyright © 萬盛學電腦網 all rights reserved