与 WooCommerce 中当前产品相关的产品类别链接术语列表 [英] Product category linked terms list related to current product in WooCommerce

查看:62
本文介绍了与 WooCommerce 中当前产品相关的产品类别链接术语列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've found online a snippet of code that currently lists all categories on this WooCommerce website.

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

How to I make it specific to show the category related to the product they are viewing?

这是我调整过的代码:

<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>';
            }
        }
    }       
}
?>
<h3>列出以下类别<?php the_category();?></h3><?php$taxonomy = 'product_cat';$orderby = '姓名';$show_count = 0;//1 表示是,0 表示否$pad_counts = 0;//1 表示是,0 表示否$hierarchical = 0;//1 表示是,0 表示否$title = '';$空 = 0;$args = 数组('分类' =>$分类法,'orderby' =>$orderby,'show_count' =>$show_count,'pad_counts' =>$pad_counts,'分层' =>$分层,'title_li' =>$title,'hide_empty' =>$空);$all_categories = get_categories( $args );foreach ($all_categories 作为 $cat) {if($cat->category_parent == 0) {$category_id = $cat->term_id;echo ' <a href="'.get_term_link($cat->slug, 'product_cat').'">'. $cat->name .'';$args2 = 数组('分类' =>$分类法,'child_of' =>0,'父母' =>$category_id,'orderby' =>$orderby,'show_count' =>$show_count,'pad_counts' =>$pad_counts,'分层' =>$分层,'title_li' =>$title,'hide_empty' =>$空);$sub_cats = get_categories( $args2 );如果($sub_cats){foreach($sub_cats 作为 $sub_category) {echo '<br><a href="'.get_term_link($sub_category->slug, 'product_cat') .'">'.$sub_category->name .'';}}}}?>

推荐答案

还有一种更简单的方法:

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

(or completely similar):

(或完全相似):

To display it as a formatted html list (with a product category links for each):

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

(or completely similar):

(或完全相似):




Explanations:

<小时>

说明:

在 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 参数,您也可以使用该参数.

As you can see in this source code, it's an alias of WordPress get_the_term_list() function with 'product_cat' as $taxonomy argument that you cans also use instead.

这篇关于与 WooCommerce 中当前产品相关的产品类别链接术语列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆