这在很大程度上取决于您的主题是如何设置的,但这里是一个总体概述:
检查你的主题的header.php 并确保body 标签看起来像:
<body <?php body_class(); ?>>
这会自动将一堆类添加到您的正文标签中,包括取决于您正在查看的类别存档页面的类。
2。使用过滤器将类别类添加到单个帖子
将以下函数插入主题的functions.php 文件:
function my_body_class_add_categories( $classes ) {
// Only proceed if we're on a single post page
if ( !is_single() )
return $classes;
// Get the categories that are assigned to this post
$post_categories = get_the_category();
// Loop over each category in the $categories array
foreach( $post_categories as $current_category ) {
// Add the current category's slug to the $body_classes array
$classes[] = 'category-' . $current_category->slug;
}
// Finally, return the $body_classes array
return $classes;
}
add_filter( 'body_class', 'my_body_class_add_categories' );
这也会将类别类添加到单个帖子中。
3。为页面添加类
body_class() 函数也可以被过滤以添加页面 slug 的类。将以下内容添加到functions.php:
function my_body_class_add_page_slug( $classes ) {
global $post;
if ( isset( $post ) ) {
$classes[] = $post->post_type . '-' . $post->post_name;
}
return $classes;
}
add_filter( 'body_class', 'my_body_class_add_page_slug' );
这会将page-title 类添加到正文中。
4。随心所欲的风格
这将根据您的主题标记而有所不同,但它会沿线:
.td-header-main-menu {
background: blue; // The fallback colour for all pages
}
.category-showbiz .td-header-main-menu {
background: red;
}
.category-sport .td-header-main-menu {
background: yellow;
}
.category-shendetsi .td-header-main-menu,
.page-shendetsi .td-header-main-menu {
background: green;
}
结论
这应该会给你大致的想法;如果不查看网站本身或不知道您使用的是哪个主题,我们无法为您提供更具体的说明。