在 WordPress 主題開發中,針對特定頁面(分類、文章類型、單頁、作者頁等)載入專屬 CSS,能避免全站載入不必要的樣式代碼,提升效能並隔離樣式作用域。本文詳細說明在 header.php 中實作條件式載入的方法、WordPress 條件標籤對照表、以及現代化替代方案。
<!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>
| 條件函式 | 適用場景 | 參數範例 |
|---|---|---|
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(
$handle, // 唯一識別名稱(不可重複)
$src, // 檔案路徑(建議用 get_template_directory_uri())
$deps = [], // 依賴的其他樣式表 handle 陣列
$ver = false, // 版本號(用於快取失效,false 為 WP 版本,null 為不輸出)
$media = 'all' // 媒體類型:all, screen, print, (max-width: 768px) 等
);
WordPress 5.9+ 引入區塊主題,樣式管理範式轉變:
wp_enqueue_style 仍相容,但建議漸進遷移至 theme.json + 區塊樣式<?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 需求。