玩的很开心!
任务是在当前嵌套级别显示子类别和这些类别中的产品的列表。
有一个代码显示忽略活动类别的所有商店类别。
$args = array(
'number' => $number,
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids,
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
echo '<h4><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h4>';
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
// 'terms' => 'white-wines'
'terms' => $product_category->slug
)
),
'post_type' => 'product',
'orderby' => 'title,'
);
$products = new WP_Query( $args );
echo "<ul>";
while ( $products->have_posts() ) {
$products->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php
}
echo "</ul>";
}
}
?>
有个理解是需要获取活动类别的ID,获取我需要的列表,但是我的php知识不足以实现这个。我挖出了这样一个代码,还有待了解如何在上述代码中正确应用它。
$current_term = get_queried_object();
$terms = get_terms( [
'taxonomy' => [ 'product_cat' ],
'parent' => $current_term->term_id,
] );
var_dump( $terms );
我希望得到您的提示,结果应该类似于以下结构:
商店 > 产品(活跃类别)
水果(儿童类别)
- 香蕉(产品)
- 苹果
- 梨
蔬菜
- 土豆
- 洋葱
- 胡椒
像这样试试
使用 get_queried_object,可以获得当前类别的对象数组。在参数中,我们使用
'parent' => $current_term->term_id,