這段代碼通過判斷 WordPress 分類 ID,自動在分類頁面和文章頁面的<body> 標籤中添加 「cat-6-id」 例如:
<body >
其中數字為該分類的 ID 號,前提是主題模板必須使用函數:
<body <?php body_class(); ?>>
有了這個非常方便的功能,你就可以針對不同的分類定義不同的樣式風格。
將以下代碼添加到您當前主題的 functions.php 文件:
function category_id_class($classes) {
     global $post;
          foreach((get_the_category($post->ID)) as $category)
               $classes [] = 'cat-' . $category->cat_ID . '-id';
     return $classes;
}
add_filter('post_class', 'category_id_class');
add_filter('body_class', 'category_id_class');

應用實例:
比如:HotNewspro 主題,在分類 ID 為 6 的分類歸檔和文章頁面隱藏熱點文章 (頂部的四張圖片),可在主題樣式文件 style 中添加:
.cat-6-id, #top_hot {
    display: none;
    }
舉一反三,可以定義其它任意位置的文字大小,背景顏色等與其它分類不同。