php - 如何按类别查询产品 woocommerce
问题描述
我在 Woocommerce 中建立一个快速的订单表格/订单表
我想要的是这样的
Main category one
Subcategory
product
product
Subcategory
product
product
Main category two
Subcategory
product
product
Subcategory
product
product
我的问题是如何在正确的类别下查询产品
这就是我到目前为止所拥有的
<?php
$custom = array(
'post_type' => 'product',
'posts_per_page' => 100,
);
$cust_query = new WP_Query( $custom );
if ( $cust_query->have_posts() ) :
while( $cust_query->have_posts() ) : $cust_query->the_post();
$cat = get_the_terms( get_the_ID(), 'product_cat' );
?>
<a href="#" class="link"><?php echo $cat[0]->name; ?></a>
<div><?php the_title();?></div>
<?php endwhile;
endif;
这是一次查询每个类别我希望所有具有相同类别的产品都在正确的类别下,并且该类别只打印一次
解决方案
试试下面的代码。在这里,我假设您只有一个级别的类别。
Main category one //level 0
Subcategory //level 1
product
product
Subcategory
product
product
Main category two //level 0
Subcategory //level 1
product
product
Subcategory
product
product
<ul class="category-sidebar">
<?php
$get_parent_cats = array(
'parent' => '0', //get top level categories only
'taxonomy'=>'product_cat',
'hide_empty' => false
);
$all_categories = get_categories( $get_parent_cats );//get parent categories
foreach( $all_categories as $single_category ){
//for each category, get the ID
$catID = $single_category->cat_ID;
echo '<li><a href=" ' . get_category_link( $catID ) . ' ">' . $single_category->name . '</a>'; //category name & link
$get_children_cats = array(
'child_of' => $catID, //get children of this parent using the catID variable from earlier
'taxonomy'=>'product_cat',
'hide_empty' => false
);
wp_reset_postdata();
$child_cats = get_categories( $get_children_cats );//get children of parent category
if(count($child_cats)>0){
echo '<ul class="children">';
foreach( $child_cats as $child_cat ){
//for each child category, get the ID
$childID = $child_cat->cat_ID;
//for each child category, give us the link and name
echo '<a href=" ' . get_category_link( $childID ) . ' ">' . $child_cat->name . '</a>';
wp_reset_postdata();
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $childID,
),
),
);
$loop = new WP_Query($args);
echo "<ul>";
if($loop->have_posts()) {
while ( $loop->have_posts() ) {
$loop->the_post();
// do something
echo "<li><a href=".get_the_permalink()."> Product Name : ".get_the_title()."</a></li>";
}
}
echo "</ul>";
}
echo '</ul></li>';
}else{
//$catID
wp_reset_postdata();
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $catID,
),
),
);
$loop = new WP_Query($args);
echo "<ul>";
if($loop->have_posts()) {
while ( $loop->have_posts() ) {
$loop->the_post();
// do something
echo "<li><a href=".get_the_permalink()."> Product Name : ".get_the_title()."</a></li>";
}
}
echo "</ul>";
}
} //end of categories logic ?>
推荐阅读
- python - 图像打开 Tkinter Python
- tensorflow - 在 CNN(Conv Neural Net)的情况下如何避免 GPU 的瓶颈?
- python-3.x - 为什么我的 ROC 曲线是反向绘制的
- powershell - Powershell GetCustomAttributes 失败,无法加载文件或程序集
- java - 我一直在尝试在地图下方获取底部表格,但我遇到了一个错误,我不知道它来自哪里
- excel - 在 Excel 中使用长度单位进行计算
- apache-poi - POI XSSF - 无法将图片添加到现有 xlsx 文件
- c++ - LLVM | 如何实现逻辑非操作的IR代码生成
- java - 为什么我在我的项目中看不到更改,但我在 fxml 文件中看到了它们?
- node.js - 使用 Sequelize 更新 NM 表值时出错