将顶级父类别显示为单个产品正文类别(WooCommerce)

时间:2021-09-14 作者:gCoffy

我从PHP开始,我正在与WooCommerce建立一个小商店。我试图在body类中显示产品的顶级父类别。

此代码运行良好:

function woo_custom_taxonomy_in_body_class( $classes ){
$custom_terms = get_the_terms(0, \'product_cat\');
if ($custom_terms) {
  foreach ($custom_terms as $custom_term) {

    // Check if the parent category exists:
    if( $custom_term->parent > 0 ) {
        // Get the parent product category:
        $parent = get_term( $custom_term->parent, \'product_cat\' );
        // Append the parent class:
        if ( ! is_wp_error( $parent ) )
            $classes[] = \'product_parent_cat_\' . $parent->slug;
    }

    $classes[] = \'product_cat_\' . $custom_term->slug;
  }
}
return $classes;
}
add_filter( \'body_class\', \'woo_custom_taxonomy_in_body_class\' );
但它只得到父类别。在这样的树上:

"E;顶级类别(>);父类别(>);类别(>);“我的产品”;

它只会得到父对象。然而,我想获得顶级类别。

你有解决办法吗?谢谢,提前了很多!

2 个回复
最合适的回答,由SO网友:Buttered_Toast 整理而成

如果您希望此功能适用于woocommerce类别,可以首先将逻辑限制为仅适用于woocommerce类别is_product_category, 这将有助于防止代码在不需要时运行。

使用WordPressget_ansestors 您可以获得当前类别的顶级等级类别(分类法),无论它有多深。

function woo_custom_taxonomy_in_body_class ($classes) {
    if (is_product_category()) {
        // get the current category (product_cat taxonomy)
        $cat = get_queried_object();

        if ($cat->parent == 0) { // if parent add it to body classes
            $classes[] = \'product_cat_\' . urldecode($cat->slug);
        } else { // if not parent, find the top leven parent and add it
            // get the category parents
            $get_ancestors = get_ancestors($cat->term_id, \'product_cat\');
            
            // get top level parent id, because ancestors from lowest to highest
            // we use PHPs end to get the last element of the array
            $top_parent_cat_id = end($get_ancestors);

            // using the parent id get the parent object
            $top_parent_cat = get_term_by(\'id\', $top_parent_cat_id, \'product_cat\');

            // get slug using the parent object
            $classes[] = \'product_parent_cat_\' . urldecode($top_parent_cat->slug);
        }
    } else if (is_product()) {
        global $product;
        
        // get the product categories and check if not empty
        if (!empty($product_categories = get_the_terms($product->get_id(), \'product_cat\'))) {
            // this is a bit more complicated
            // because product can have multiple categories we cannot always get the correct one
            // for this example ill take the firdt category I find and work with it
            // if you only have one category per product then this will work fine
            
            // get the first category (product_cat taxonomy) that we find
            $cat = $product_categories[0];

            if ($cat->parent == 0) { // if parent add it to body classes
                $classes[] = \'product_cat_\' . urldecode($cat->slug);
            } else { // if not parent, find the top leven parent and add it
                // get the category parents
                $get_ancestors = get_ancestors($cat->term_id, \'product_cat\');

                // get top level parent id, because ancestors from lowest to highest
                // we use PHPs end to get the last element of the array
                $top_parent_cat_id = end($get_ancestors);

                // using the parent id get the parent object
                $top_parent_cat = get_term_by(\'id\', $top_parent_cat_id, \'product_cat\');

                // get slug using the parent object
                $classes[] = \'product_parent_cat_\' . urldecode($top_parent_cat->slug);
            }
        }
    }

    return $classes;
}
add_filter(\'body_class\', \'woo_custom_taxonomy_in_body_class\');

SO网友:gCoffy

编辑:我可能找到了一个基于@Buttered\\u Toast answer的解决方案(在正文和产品框中显示类(我需要该类位于产品框、店铺正文和产品页面正文中)):

function woo_custom_taxonomy_in_body_class ($classes) {
    if (is_woocommerce()) {
    // get the product categories and check if not empty
    if (!empty($product_categories = get_the_terms(0, \'product_cat\'))) {
        // this is a bit more complicated
        // because product can have multiple categories we cannot always get the correct one
        // for this example ill take the firdt category I find and work with it
        // if you only have one category per product then this will work fine
        
        // get the first category (product_cat taxonomy) that we find
        $cat = $product_categories[0];

        if ($cat->parent == 0) { // if parent add it to body classes
            $classes[] = \'product_cat_\' . urldecode($cat->slug);
        } else { // if not parent, find the top leven parent and add it
            // get the category parents
            $get_ancestors = get_ancestors($cat->term_id, \'product_cat\');

            // get top level parent id, because ancestors from lowest to highest
            // we use PHPs end to get the last element of the array
            $top_parent_cat_id = end($get_ancestors);

            // using the parent id get the parent object
            $top_parent_cat = get_term_by(\'id\', $top_parent_cat_id, \'product_cat\');

            // get slug using the parent object
            $classes[] = \'product_parent_cat_\' . urldecode($top_parent_cat->slug);
        }
        }
    }
return $classes;
}
add_filter( \'post_class\', \'woo_custom_taxonomy_in_body_class\' );
add_filter(\'body_class\', \'woo_custom_taxonomy_in_body_class\');
这是正确的吗?就我个人而言,我进行了测试,结果很有效。当做