将 CSS 应用于一个类别(及其子类别)



最终,我想为一个类别(及其子类别(隐藏或禁用相关产品。我有以下代码,但它不起作用,我无法找出问题所在。

function hideRelated () {
if ( is_product_category(18) ) {
?>
<style>
.woocommerce .related.products { display: none; }
</style>
<?php
}
}
add_filter('wp_head', 'hideRelated');

如果这对其他人有帮助,下面的代码对我有效:

// 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作为一个主体类,然后可以在CSS中调用它。

最新更新