与 WooCommerce 中当前产品相关的产品类别链接术语列表

Ant*_*eld 0 php wordpress product categories woocommerce

我在网上找到了一段代码,目前列出了这个 WooCommerce 网站上的所有类别。

如何使其具体显示与他们正在查看的产品相关的类别?

这是我调整过的代码:

<div id="ListCat">                  
<h3>Listed in the following categories<?php the_category(); ?></h3>
<?php
$taxonomy     = 'product_cat';
$orderby      = 'name';  
$show_count   = 0;      // 1 for yes, 0 for no
$pad_counts   = 0;      // 1 for yes, 0 for no
$hierarchical = 0;      // 1 for yes, 0 for no  
$title        = '';  
$empty        = 0;

$args = array(
     'taxonomy'     => $taxonomy,
     'orderby'      => $orderby,
     'show_count'   => $show_count,
     'pad_counts'   => $pad_counts,
     'hierarchical' => $hierarchical,
     'title_li'     => $title,
     'hide_empty'   => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
    $category_id = $cat->term_id;       
    echo ' <a href="'. get_term_link($cat->slug, 'product_cat') 
.'">'. $cat->name .'</a>';

    $args2 = array(
        'taxonomy'     => $taxonomy,
        'child_of'     => 0,
        'parent'       => $category_id,
        'orderby'      => $orderby,
        'show_count'   => $show_count,
        'pad_counts'   => $pad_counts,
        'hierarchical' => $hierarchical,
        'title_li'     => $title,
        'hide_empty'   => $empty
    );
   $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            foreach($sub_cats as $sub_category) {
                echo  '<br><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';
            }
        }
    }       
}
?>
Run Code Online (Sandbox Code Playgroud)

Loi*_*tec 6

还有一个更简单的方法:

要将其显示为逗号分隔的字符串 (每个都有一个产品类别链接)

// Display a coma separated string of the product categories for this product
echo wc_get_product_category_list( get_the_id() );
Run Code Online (Sandbox Code Playgroud)

(或完全相似)

// Display a coma separated string of the product categories for this product
echo get_the_term_list( get_the_id(), 'product_cat' );
Run Code Online (Sandbox Code Playgroud)

要将其显示为格式化的 html 列表 (每个列表都有一个产品类别链接)

// Display a html formatted list of the product categories for this product
echo '<ul>' . wc_get_product_category_list( get_the_id(), '</li><li>', '<li>',  '</li>' ) . '</ul>';
Run Code Online (Sandbox Code Playgroud)

(或完全相似)

// Display a html formatted list of the product categories for this product
echo '<ul>' . get_the_term_list( get_the_id(), 'product_cat', '<li>', '</li><li>', '</li>' ) . '</ul>';
Run Code Online (Sandbox Code Playgroud)

说明:

在 Woocommerce 3 中,有一个wc_get_product_category_list()专门的函数可以从产品 ID 中列出产品类别,上面有一些可用的参数,就像你在它的源代码中看到的那样:

/**
 * Returns the product categories in a list.
 *
 * @param int $product_id
 * @param string $sep (default: ', ').
 * @param string $before (default: '').
 * @param string $after (default: '').
 * @return string
 */
function wc_get_product_category_list( $product_id, $sep = ', ', $before = '', $after = '' ) {
    return get_the_term_list( $product_id, 'product_cat', $before, $sep, $after );
}
Run Code Online (Sandbox Code Playgroud)

正如您在此源代码中所见,它是 WordPressget_the_term_list()函数的别名,您也可以使用'product_cat'as$taxonomy参数来代替。