You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:无需插件为WooCommerce创建自定义Taxonomy的解决方案

WooCommerce 自定义分类法(无需插件)解决方案

要给WooCommerce产品添加品牌(brands)、材质(materials)这类自定义分类,不用插件的话,核心就是把自定义分类法关联到WooCommerce的产品文章类型product上——你原来的代码绑定的是自定义文章类型wpp_shoe,替换这个参数就能适配。

下面是适配后的完整代码,包含品牌和材质两个分类的注册:

add_action('init', 'register_woocommerce_custom_taxonomies');
function register_woocommerce_custom_taxonomies() {
    // 注册品牌分类法
    $brand_labels = array(
        'name'              => _x('品牌', 'taxonomy general name'),
        'singular_name'     => _x('品牌', 'taxonomy singular name'),
        'search_items'      => __('搜索品牌'),
        'all_items'         => __('所有品牌'),
        'parent_item'       => __('父品牌'),
        'parent_item_colon' => __('父品牌:'),
        'edit_item'         => __('编辑品牌'),
        'update_item'       => __('更新品牌'),
        'add_new_item'      => __('添加新品牌'),
        'new_item_name'     => __('新品牌名称'),
        'menu_name'         => __('品牌'),
    );
    $brand_args = array(
        'hierarchical'      => true, // 层级结构(支持父品牌,和默认产品分类逻辑一致)
        'labels'            => $brand_labels,
        'show_ui'           => true,
        'show_admin_column' => true, // 在产品列表页显示该分类列
        'query_var'         => true,
        'show_in_rest'      => true, // 支持古腾堡编辑器
        'rewrite'           => array('slug' => 'brand'), // 前端访问的URL后缀
    );
    register_taxonomy('brand', 'product', $brand_args); // 关联到WooCommerce产品类型

    // 注册材质分类法
    $material_labels = array(
        'name'              => _x('材质', 'taxonomy general name'),
        'singular_name'     => _x('材质', 'taxonomy singular name'),
        'search_items'      => __('搜索材质'),
        'all_items'         => __('所有材质'),
        'parent_item'       => __('父材质'),
        'parent_item_colon' => __('父材质:'),
        'edit_item'         => __('编辑材质'),
        'update_item'       => __('更新材质'),
        'add_new_item'      => __('添加新材质'),
        'new_item_name'     => __('新材质名称'),
        'menu_name'         => __('材质'),
    );
    $material_args = array(
        'hierarchical'      => true,
        'labels'            => $material_labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'show_in_rest'      => true,
        'rewrite'           => array('slug' => 'material'),
    );
    register_taxonomy('material', 'product', $material_args);
}

操作步骤:

  1. 把上述代码复制到当前主题的functions.php文件末尾,或者创建一个简单的自定义插件存放代码(推荐后者,避免主题更新丢失代码)。
  2. 登录WordPress后台,进入「设置」→「固定链接」,点击「保存更改」刷新固定链接规则,确保自定义分类的URL能正常访问。
  3. 现在你可以在产品编辑页面看到「品牌」和「材质」的分类选项,和默认的产品分类、标签一样使用,直接添加Nike、Adidas到品牌,leather、canvas到材质即可。

内容的提问来源于stack exchange,提问作者user22118401

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 20:42:42