我认为这个函数可以生成子类别woocommerce_product_subcategories();
. 但正如您所说,对于这个布局,没有特定的模板文件。而且,如果我没记错的话,你不能单独用css来实现这一点。
不久前,我为一位客户创建了一个小插件,该客户还需要一个由您想要的产品类别构成的产品概述。
插件方式是一种方式。
插件解决方案的好处是,你可以在任何地方显示你的类别/产品列表,在那里你可以添加短代码。(贴子、页面、小部件),如果您只想用产品显示单个类别,或者不想显示计数或描述,也可以添加参数。我还使我的列表可折叠,以便客户只能折叠单个类别,并可以隐藏其他类别。
另一种方法,如您所述,是编辑archiv-product.php
方法
要执行此操作,请首先复制archiv-product.php
从woocommerce插件目录进入主题目录,在一个名为woocommerce
.之后,您可以编辑archiv-product.php
主题目录中的文件。
对于这两种方式,您都需要这样的逻辑:(在进行过程中,我尽了最大努力对代码进行注释)
// first we need to get the product category terms ....
$categories_args = array(
\'taxonomy\' => \'product_cat\' // the taxonomy we want to get terms of
//\'parent\' => 0, // only show terms if parent is 0 = top level terms, try it
);
$product_categories = get_terms( $categories_args ); // get all terms of the product category taxonomy
if ($product_categories) { // only start if there are some terms
echo \'<ul class="catalog">\';
// now we are looping over each term
foreach ($product_categories as $product_category) {
$term_id = $product_category->term_id; // single term ID
$term_name = $product_category->name; // single term name
$term_desc = $product_category->description; // single term description
$term_link = get_term_link($product_category->slug, $product_category->taxonomy); // single term link
echo \'<li class="product-cat-\'.$term_id.\'">\'; // for each term we will create one list element
echo \'<h4 class="product-cat-title"><a href="\'.$term_link.\'">\'.$term_name.\'</a></h4>\'; // display term name with link
echo \'<p class="product-cat-description">\'.$term_desc .\'</p>\'; // display term description
// ... now we will get the products which have that term assigned...
$products_args = array(
\'post_type\' => \'product\', // we want to get products
\'tax_query\' => array(
array(
\'taxonomy\' => \'product_cat\', // the product taxonomy
\'field\' => \'term_id\', // we want to use the term_id not slug
\'terms\' => $term_id, // here we enter the ID of the current term *this is where the magic happens*
),
),
);
$products = new WP_Query( $products_args );
if ( $products->have_posts() ) { // only start if we hace some products
// START some normal woocommerce loop, as you already posted in your question
woocommerce_product_loop_start(); // this will generate the start of the default products list UL
while ( $products->have_posts() ) : $products->the_post();
wc_get_template_part( \'content\', \'product\' ); // we are using the default product template from woocommerce
endwhile; // end of the loop.
woocommerce_product_loop_end(); // this generates the end of the default woocommerce product list UL
// 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
echo \'</li>\';//END here is the end of our product-cat-term_id list item
}//END foreach $product_categories
echo \'</ul>\';//END of catalog list
}//END if $product_categories
问题是,你不能用上面的代码片段来替换普通的woo循环(你发布的代码)。您还需要删除
archiv-product.php
文件
我创建了一个全面工作的要点archiv-product.php
文件here.我想这会对你有帮助。
还要看get_terms()
作用here, 和WP查询参数here. 我只是使用了我们需要的基本论点,当然你还需要更多的论点。