【发布时间】:2016-08-06 03:51:33
【问题描述】:
有没有一种简单的方法可以为 Woocommerce 中的特定产品添加自定义样式?例如,我希望类别为“Category1”的所有产品都具有蓝色页面背景颜色,而类别为“Category2”的所有产品都具有白色页面背景颜色。
不幸的是,我对 PHP 几乎一无所知。你能像我五年级一样向我解释解决方案吗?
提前致谢:)
【问题讨论】:
标签: woocommerce
有没有一种简单的方法可以为 Woocommerce 中的特定产品添加自定义样式?例如,我希望类别为“Category1”的所有产品都具有蓝色页面背景颜色,而类别为“Category2”的所有产品都具有白色页面背景颜色。
不幸的是,我对 PHP 几乎一无所知。你能像我五年级一样向我解释解决方案吗?
提前致谢:)
【问题讨论】:
标签: woocommerce
您需要在主题的 functions.php 文件中添加一个函数:
// 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' );
这个函数是cribbed from here.
此函数会将产品类别 slug 作为类名添加到 <body> 元素,这将允许您专门针对该页面定位样式。
【讨论】: