在产品详细信息页面的WooCommerce中按类别ID获取类别URL

Jus*_*n K 4 php wordpress product woocommerce hook-woocommerce

我在产品详细信息页面上查找类别URL.我使用下面的代码来获取类别ID,但我不知道如何获取类别URL.

<?php
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
    echo $product_cat_id = $term->term_id;
    break;
}
Run Code Online (Sandbox Code Playgroud)

Bas*_*ein 5

如果您有产品类别id,则可以使用

$link = get_term_link( $product_cat_id, 'product_cat' );
Run Code Online (Sandbox Code Playgroud)

获取产品类别的网址.

编辑确保product_cat_id是一个整数,使用以下行完全保存:

$link = get_term_link( (int)$product_cat_id, 'product_cat' );
Run Code Online (Sandbox Code Playgroud)