问题描述
是否有一种简单的方法可以为 Woocommerce 中的特定产品添加自定义样式?例如,我希望类别为Category1"的所有产品的页面背景颜色为蓝色,类别为类别 2"的所有产品的页面背景颜色为白色.
Is there an easy way to add a custom style to a specific products in Woocommerce? For example, I want all products with a category of 'Category1' to have a blue page background color, and all products with a category of 'Category2' to have a white page background color.
不幸的是,我对 PHP 一无所知.你能像我是五年级学生一样向我解释解决方案吗?
Unfortunately, I am mostly clueless when it comes to PHP. Can you explain the solution to me like I'm a 5th grader?
提前致谢:)
推荐答案
您需要在主题的 functions.php
文件中添加一个函数:
You'll need to ad a function to your theme's functions.php
file:
// add taxonomy term to body_class
function woo_custom_taxonomy_in_body_class( $classes ){
if( is_singular( 'product' ) )
{
$custom_terms = get_the_terms(0, 'product_cat');
if ($custom_terms) {
foreach ($custom_terms as $custom_term) {
$classes[] = 'product_cat_' . $custom_term->slug;
}
}
}
return $classes;
}
add_filter( 'body_class', 'woo_custom_taxonomy_in_body_class' );
此函数从这里抄袭.此函数会将产品类别 slug 作为类名添加到 元素,这将允许您针对该页面专门定位样式.
This function is cribbed from here.This function will add the product category slug as a class name to the <body>
element, which will allow you to target the styles specifically for that page.
这篇关于特定产品类别的 Woocommerce 自定义样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!