首页 > 解决方案 > 在 Woocommerce 产品类别存档页面中获取当前术语名称

问题描述

我正在尝试获取产品类别存档页面的单个产品类别名称。

使用此代码:

$categ = $product->get_categories(); 
echo $categ;

它还显示了所有父类别……</p>

如何在 Woocommerce 产品类别存档页面中获取当前术语名称?

标签: phpwordpresswoocommercecustom-taxonomy

解决方案


要获取当前产品类别存档页面的产品类别术语名称,您将使用:

// Only on product category archive pages
if(is_product_category()){
    // The WP_Term object for the current product category
    $term = get_queried_object();

    // Get the current term name for the product category page
    $term_name = $term->name;

    // Test output
    echo $term->name . '<br>';
}

测试和工作。


推荐阅读