如何调整WooCommerce产品页商品分类的默认A-Z排序为自定义优先级排序
WooCommerce商品分类自定义排序实现方案
方案一:使用自带拖拽排序功能(无代码,推荐普通用户使用)
- 进入WordPress后台,依次点击「产品」→「产品分类」菜单
- 鼠标悬停在需要调整顺序的分类条目上,按住左键上下拖拽即可调整分类优先级,拖拽位置越靠前优先级越高
- 调整完成后前台产品页的分类会自动按照该顺序展示,无需额外配置
方案二:代码自定义优先级(适合需要灵活控制排序规则的场景)
将以下代码添加到当前使用主题的functions.php文件,或者通过自定义代码片段插件添加即可生效:
// 自定义WooCommerce商品分类显示顺序 add_filter( 'get_the_terms', 'custom_sort_product_categories', 10, 3 ); function custom_sort_product_categories( $terms, $post_id, $taxonomy ) { // 仅对产品分类 taxonomy 生效 if ( $taxonomy !== 'product_cat' || get_post_type( $post_id ) !== 'product' ) { return $terms; } // 自定义分类优先级配置:键为分类别名,数值越小排序越靠前 $custom_priority = [ 'intel' => 1, 'cpu' => 2, // 可按需添加更多分类的优先级规则 ]; usort( $terms, function( $a, $b ) use ( $custom_priority ) { $priority_a = isset( $custom_priority[$a->slug] ) ? $custom_priority[$a->slug] : 999; $priority_b = isset( $custom_priority[$b->slug] ) ? $custom_priority[$b->slug] : 999; return $priority_a - $priority_b; } ); return $terms; }
注意:修改主题
functions.php前建议提前备份网站,避免语法错误导致网站异常。
- 代码中的
$custom_priority数组可根据你的实际分类需求调整,未配置优先级的分类会默认排在已配置分类之后
内容的提问来源于stack exchange,提问作者KalaHurry
相关产品推荐
相关产品推荐

