Woocommerce实现:在产品分类页顶部展示特色产品
嘿,这个需求我帮很多人解决过,不用插件完全可以靠代码实现,而且有两种稳妥的方式,你可以根据自己的技术习惯选:
方法一:使用钩子(无需修改模板文件,推荐)
这种方式最灵活,不用动主题的模板文件,直接把代码加到主题的functions.php或者自定义插件里(强烈推荐用自定义插件,避免主题更新时丢失代码):
add_action( 'woocommerce_before_shop_loop', 'display_random_featured_products_in_category', 10 ); function display_random_featured_products_in_category() { // 只在产品分类页执行 if ( is_product_category() ) { // 获取当前分类的ID $current_cat_id = get_queried_object_id(); // 组装查询参数:随机取3个当前分类下的特色产品 $args = array( 'post_type' => 'product', 'posts_per_page' => 3, 'orderby' => 'rand', // 核心:随机排序 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $current_cat_id, 'include_children' => false, // 可选:true则包含子分类产品,按需调整 ) ), 'meta_query' => array( array( 'key' => '_featured', 'value' => 'yes', // WooCommerce标记特色产品的元字段 ) ) ); $featured_products = new WP_Query( $args ); if ( $featured_products->have_posts() ) { echo '<div class="featured-products-top">'; echo '<h3>精选特色产品</h3>'; // 这里可以自定义标题 echo '<div class="featured-products-grid">'; // 循环输出产品,复用WooCommerce自带的产品模板 while ( $featured_products->have_posts() ) { $featured_products->the_post(); wc_get_template_part( 'content', 'product' ); } echo '</div></div>'; // 重置查询,避免影响后续的归档循环 wp_reset_postdata(); } } }
这段代码的优势:
- 自动复用你现有主题的产品样式,不用自己写HTML结构
- 只在产品分类页生效,不会影响其他页面
- 可以通过调整
include_children来决定是否包含子分类的产品
方法二:直接修改产品分类模板文件
如果你习惯直接操作模板文件,先找到主题(或子主题)里的taxonomy-product_cat.php(如果没有,就复制WooCommerce默认的archive-product.php,重命名为taxonomy-product_cat.php),在产品归档循环开始之前(比如<?php if ( woocommerce_product_loop() ) : ?>这行代码的上方)插入以下代码:
<?php // 获取当前分类ID $current_cat_id = get_queried_object_id(); // 组装特色产品查询参数 $featured_args = array( 'post_type' => 'product', 'posts_per_page' => 3, 'orderby' => 'rand', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $current_cat_id, 'include_children' => false, ) ), 'meta_query' => array( array( 'key' => '_featured', 'value' => 'yes', ) ) ); $featured_products = new WP_Query( $featured_args ); if ( $featured_products->have_posts() ) : ?> <div class="featured-products-top"> <h3>我们的特色精选</h3> <div class="featured-products-grid"> <?php while ( $featured_products->have_posts() ) : $featured_products->the_post(); ?> <?php wc_get_template_part( 'content', 'product' ); ?> <?php endwhile; ?> </div> </div> <?php // 重置查询,确保后续归档循环正常 wp_reset_postdata(); endif; ?>
必看的补充提示
- 子主题优先:不管用哪种方法,都要用子主题来修改,否则主题更新后你的代码会直接丢失!
- 样式调整:代码里加了
featured-products-top和featured-products-grid两个类,你可以自己写CSS调整布局,比如让3个产品横向排列:
.featured-products-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; margin-bottom: 30px; } /* 移动端适配 */ @media (max-width: 768px) { .featured-products-grid { grid-template-columns: 1fr; } }
- 非WooCommerce场景:如果你的产品是自定义文章类型,只需要把
post_type改成你的自定义类型,taxonomy改成对应的分类法,特色产品的判断换成你自己的自定义字段(比如is_featured)即可。 - 性能优化:如果你的产品数量很多,
orderby=rand可能会有轻微性能影响,你可以用transient缓存查询结果,比如缓存1小时:
// 替换原来的$featured_products = new WP_Query( $args ); $transient_key = 'featured_products_cat_' . $current_cat_id; $featured_products = get_transient( $transient_key ); if ( false === $featured_products ) { $featured_products = new WP_Query( $args ); set_transient( $transient_key, $featured_products, 3600 ); // 缓存1小时 }
内容的提问来源于stack exchange,提问作者Waloob73
相关产品推荐
相关产品推荐

