让我们看看这是否有用。根据您的描述,您有/想要以下设置:
-Category
--Sub-Category-1
---Products of Sub-Category-1
--Sub-Category-2
---Products of Sub-Category-2
--Sub-Category-3
---Products of Sub-Category-3
好的,那么您首先需要获取所有顶级类别。您可以通过以下方式获得它们:
$top_categories_args = array(
\'taxonomy\' => \'product_cat\', // the taxonomy we want to get terms of
\'parent\' => 0 // all top level cats with a parent of 0
);
$top_categories = get_terms( $top_categories_args );
通过foreach循环迭代所有顶级CAT:
foreach ($top_categories as $top_category) {
$top_id = $top_category->term_id; // get term ID
$top_slug = $top_category->slug; // get term slug
$top_name = $top_category->name; // get term title
$top_desc = $top_category->description; // get term description
echo \'<div class="\'.$top_slug.\'">\';
echo \'<h2>\'.$top_name.\'</h2>\';
if ($top_desc) {
echo \'<p>\'.$top_desc.\'</p>\';
}
// here we now need to get all the sub-categories
echo \'</div><!-- END top categories container -->\';
}
现在我们需要一个类似的新
get_terms
查询子类别:(应替换”//此处我们现在需要获取所有子类别)
// here we get all the sub categories of the current cat
$sub_categories_args = array(
\'taxonomy\' => \'product_cat\',
\'parent\' => $top_id // we use the top_id from before to get only sub-cats
);
$sub_categories = get_terms( $sub_categories_args );
foreach ($sub_categories as $sub_category) {
$sub_id = $sub_category->term_id;
$sub_slug = $sub_category->slug;
$sub_name = $sub_category->name;
$sub_desc = $sub_category->description;
echo \'<div class="\'.$top_slug.\'-\'.$sub_slug.\'">\';
echo \'<h3>\'.$sub_name.\'</h3>\';
if ($sub_desc) {
echo \'<p>\'.$sub_desc.\'</p>\';
}
// here we now need to get all the products inside this sub-categories
echo \'</div><!-- END sub categories container -->\';
}
现在,在这个子类别代码中,您现在可以通过结合tax\\u查询的查询来获取产品。(应替换“/”,我们现在需要获取此子类别中的所有产品)
$products_args = array(
\'post_type\' => \'product\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'term_id\', // we look for the ID, you could also use slug
\'terms\' => $sub_id, // get only products with the sub-cat ID
),
),
);
$products = new WP_Query( $products_args );
if ( $products->have_posts() ) { // only start if we hace some products
// START some normal woocommerce loop
woocommerce_product_loop_start();
while ( $products->have_posts() ) : $products->the_post();
wc_get_template_part( \'content\', \'product\' );
endwhile; // end of the loop.
woocommerce_product_loop_end();
// END the normal woocommerce loop
// Restore original post data, maybe not needed here (in a plugin it might be necessary)
wp_reset_postdata();
} else { // if we have no products, show the default woocommerce no-product loop
// no posts found
wc_get_template( \'loop/no-products-found.php\' );
}//END if $products
我在这里创建了一个要点,以及完整的产品档案。php和更多详细信息:
product-archive.php如果您有一些不想显示的术语,可以使用exclude
或exclude_tree
, 禁用它们。有关更多选项,请查看codex页面here.
// first we get all top-level categories
$top_categories_args = array(
\'taxonomy\' => \'product_cat\', // the taxonomy we want to get terms of
\'parent\' => 0, // all top level cats with a parent of 0
\'hide_empty\' => true,
\'exclude\' => \'11,23,99\', // Array or comma/space-separated string of term ids to exclude.
\'exclude_tree\' => \'2,5,12\' // Array or comma/space-separated string of term ids to exclude along with all of their descendant terms.
);
您还可以使用WooCommerce的默认拖放功能更改条款的顺序。只需在后端拖放术语。