【问题标题】:Add body class to category with all its subcategories | Woocommerce将 body 类添加到包含其所有子类别的类别 |电子商务
【发布时间】:2016-12-03 15:07:17
【问题描述】:

我想在查看类别及其所有子类别时将类添加到正文标记,下面的代码可以工作,但它只是将类添加到相关类别而不是其所有子类别:

add_filter( 'body_class','custom_body_class' );
function custom_body_class( $classes ) {

    if ( is_product_category( 802 ) ) {
        $classes[] = 'class1';
    } else if ( is_product_category( array( 'femme', 'homme', 'enfant' ) ) ) {
        $classes[] = 'class2';
    }

    return $classes;

}

我尝试将 is_product_category 替换为 in_category 或 is_product_tag 但它也不起作用。

任何帮助将不胜感激。

【问题讨论】:

  • 我不确定我是否正确理解了您的问题。您想将 css 类添加到类别页面及其所有子类别页面吗?也就是说,我有一个名为 Fruit 的类别和子类别 Apple、Lemon...你希望 Apple 和 Lemon 的子类别页面的主体上都有 CSS 类名称 Fruit?
  • 是的 LoicTheAztec 给了我很好的解决方案,谢谢 ucheng!

标签: php wordpress woocommerce categories product


【解决方案1】:

您需要改为使用相关的 WordPress 条件函数 has_term()'product_cat' 分类参数来以这种方式定位产品类别:

add_filter( 'body_class','custom_body_class' );
function custom_body_class( $classes ) {

    if ( has_term( 802, 'product_cat' ) ) { // assuming that 802 is the id of a product category.
        $classes[] = 'class1';
    } else if ( has_term( array( 'femme', 'homme', 'enfant' ), 'product_cat' ) ) {
        $classes[] = 'class2';
    }

    return $classes;

}

更新 2(根据您的评论):

当存档类别(或子类别)页面为空时,可以使用 get_queried_object() 函数获取当前术语对象并调整函数中的条件以处理这些情况。

代码如下:

add_filter( 'body_class','custom_body_class' );
function custom_body_class( $classes ) {

    // Only on product category (or subcategory) archives pages     
    if(is_product_category()):

    // Set HERE your Main category ID (the parent ID of subcategories IDs)
    $main_cat = 802;

    // Set HERE your subcategories slugs
    $sub_cats_arr = array( 'femme', 'homme', 'enfant' );

    // Getting the current term (object) of category or subcategory archives page 
    $term = get_queried_object();

    // All the needed data from current term (object)
    $term_id = $term->term_id; // term ID
    $term_slug = $term->slug; // term slug
    $term_name = $term->name; // term name
    $term_taxonomy = $term->taxonomy; // Always 'product_cat' for woocommerce product categories
    $term_parent_id = $term->parent; // the parent term_id

    // FOR YOUR MAIN CATEGORY ID '802' (Handle empty archive page case too)
    if ( has_term( $main_cat, $term_taxonomy ) || $term_id == $main_cat )
        $classes[] = 'class1';

    // FOR YOUR MAIN 3 SUBCATEGORIES SLUGS (Handle empty archive page case too)
    if ( has_term( $sub_cats_arr, $term_taxonomy ) || in_array($term_slug, $sub_cats_arr) )
            $classes[] = 'class2';

    endif;

    return $classes;

}

这样,您应该避免在查看类别或子类别时出现不包含不添加您的 body 自定义类的产品的情况......

【讨论】:

  • 查看分类或子分类时,没有产品,分类不加,正常吗?有没有办法添加该类别无论如何该类别是否有产品?
  • @colapsnux has_term() 仅适用于作为产品的帖子或自定义帖子......所以让我想想......对于没有产品的类别,您可以在条件下将 has_term() 与 is_product_category() 结合使用“或” “……但是对于没有产品的子类别,我还不知道……在给出可行的解决方案之前,我必须进行一些测试。
  • @colapsnux 我已经更新了我的答案……试试看然后告诉我。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
  • 2023-03-17
相关资源
最近更新 更多