body_class函数非常适合在body标签中添加一堆类,这些类具有有关当前正在显示哪种页面的信息。 可能是出于样式目的。 但是无论出于何种原因,它都不会为单个帖子包含当前类别的一个或多个类别。
这将添加“ nice”类别名称:
add_filter('body_class','add_category_to_single');
function add_category_to_single($classes) {
if (is_single() ) {
global $post;
foreach((get_the_category($post->ID)) as $category) {
// add category slug to the $classes array
$classes[] = $category->category_nicename;
}
}
// return the $classes array
return $classes;
}
翻译自: https://css-tricks.com/snippets/wordpress/add-category-name-body_class/