如何添加与产品类别slug相关的body类?
谢谢!
将其添加到您的函数中。php
add_filter( 'body_class', 'wc_cat_names' );
function wc_cat_names( $classes ) {
if(is_product()){
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$classes[] = $term->slug;
}
}
return $classes;
}
这个将只在wooccommerce产品页面上工作,是的,我在我的测试网站上测试了这个。
Wordpress支持网站上有一个很好的指南:修改body_class()输出以显示单帖子的类别-{slug}
我已经修改了帖子中的代码以满足您的需求:
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) {
echo $category->cat_name . ' ';
// add category slug to the $classes array
$classes[] = $category->slug;
}
}
// return the $classes array
return $classes;
}