在WooCommerce中获取当前产品类别

时间:2014-04-04 04:17:29

标签: php woocommerce

我想获取我的WooCommerce产品的当前类别,并将其显示在我的产品下方。目前,它按字母顺序获取类别。当你有一个名为“许可”的父类别和一个名为“鞋子”的子类别时,这有点奇怪,即使在去鞋类别时它也会获得许可。

现在主题中内置的功能发布在下面。

function gbx_wc_get_cat_forproduct() {
global $smof_data, $product;

if (!$smof_data['show_shop_category'])
    return;

$terms = get_the_terms($product->ID, 'product_cat');
if ($terms && !is_wp_error($terms)) {
    foreach ($terms as $term) {
        echo '<a class="product-cat" href="' . get_term_link($term->slug, 'product_cat') . '">' . $term->name . '</a>';
        break;
    }
}
}

我用谷歌搜索了如何做到这一点,但我的PHP知识至少可以说是有限的。我也看到了另一个问题,但它似乎没有帮助我。 --- WooCommerce - get category for product page

Example of issue

0 个答案:

没有答案
相关问题