【发布时间】:2018-01-24 21:40:02
【问题描述】:
我在网上找到了一段代码,目前列出了这个 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>';
}
}
}
}
?>
【问题讨论】:
-
知道如何在您提供的 sn-p 中识别产品,很难知道您所提问题的答案。
标签: php wordpress woocommerce categories product