【问题标题】:Apply CSS for One Cateory (and it's sub-categories)将 CSS 应用于一个类别(及其子类别)
【发布时间】:2022-01-23 07:32:01
【问题描述】:

最终,我想隐藏或禁用一个类别(以及它的子类别)的相关产品。我有以下代码,但它不起作用,我不知道出了什么问题。

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

【问题讨论】:

  • 首先,is_product_category 希望您传递类别 slug 而不是类别 ID。 REFERENCE。其次,您要在哪个页面上运行它?在category archive 页面上?在single product 页面上?你能说得更具体点吗?
  • 感谢您的提示。我会用蛞蝓。我希望它在单个产品页面上运行。

标签: woocommerce


【解决方案1】:

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

// 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 添加为 body 类 - 然后可以在 CSS 中调用。

【讨论】:

    猜你喜欢
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多