代码之家  ›  专栏  ›  技术社区  ›  Brian Duncan

如何在WordPress/WooCommerce中将CSS应用于除某些产品页面之外的所有页面?

  •  2
  • Brian Duncan  · 技术社区  · 6 年前

    我想知道如何将这个CSS代码应用到我网站上的每一页,除了WooCommerce产品类别为“类别1”。

    .custom-single-product .cart, span.woocommerce-Price-amount.amount, a.cart-customlocation.et-cart-info {
        display: none !important;
    }
    

    我尝试用下面的代码用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' );
    

    基本上,最终目标是将某些产品添加到购物车,显示价格和结帐,但将这些选项和信息隐藏在绝大多数产品和网站上。 谢谢

    1 回复  |  直到 6 年前
        1
  •  3
  •   Cave Johnson    6 年前

    添加 body:not(.product_cat_Category1) 到每个CSS规则。所以应该是这样的:

    body:not(.product_cat_Category1) .custom-single-product .cart, body:not(.product_cat_Category1) span.woocommerce-Price-amount.amount, body:not(.product_cat_Category1) a.cart-customlocation.et-cart-info {
        display: none !important;
    }