我在这里找到了一些将类别作为类添加到主体中的代码:https://css-tricks.com/snippets/wordpress/add-category-name-body_class/但它似乎只添加了一个类别。有谁知道如何调整此代码,以便它可以向正文添加多个类别类?
add_filter('body_class','add_category_to_single');
function add_category_to_single($classes, $class) {
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;
}
最佳答案
解决方案
您可以将此代码添加到自定义的functions.php 文件中:
function add_categories( $classes = '' ) {
$categories = get_the_category();
foreach( $categories as $category ) {
$classes[] = 'category-'.$category->slug;
}
return $classes;
}
add_filter( 'body_class', 'add_categories' );
关于wordpress - 向 WordPress 主体类添加多个类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45972062/