仅列出与WooCommerce中当前产品相关的产品类别

时间:2017-08-16 16:30:04

标签: php wordpress woocommerce categories product

我在网上找到了一段代码,目前列出了这个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>';
            }
        }
    }       
}
?>

2 个答案:

答案 0 :(得分:1)

  

有一种更简单的方式:

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

// Display a coma separated string of the product categories for this product
echo wc_get_product_category_list( get_the_id() );

(或完全相似)

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

将其显示为格式化的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>';

(或完全相似)

// 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>';

<强>说明:

在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 );
}
  

正如您在此源代码中所看到的,它是WordPress get_the_term_list()函数的别名,'product_cat'作为$taxonomy参数,您也可以使用它。

答案 1 :(得分:0)

如果您需要不带类别链接的链接,请使用此代码

foreach((get_the_terms( $post->ID, 'product_cat' )) as $category) {
    echo $category->name . '</br>';
}
相关问题