自定义 Woocommerce 标签内置结构问题

Customizing Woocommerce tags built-in structure issue

我正在尝试使用以下方法将 Woocommerce 标签更改为分层:

    function wd_hierarchical_tags_register() {

    $labels = array(
        'name'                       => 'Items',
        'singular_name'              => 'Item',
        'menu_name'                  => 'Item',
        'all_items'                  => 'All Items',
        'parent_item'                => 'Parent Item',
        'parent_item_colon'          => 'Parent Item:',
        'new_item_name'              => 'New Item Name',
        'add_new_item'               => 'Add New Item',
        'edit_item'                  => 'Edit Item',
        'update_item'                => 'Update Item',
        'separate_items_with_commas' => 'Separate Item with commas',
        'search_items'               => 'Search Items',
        'add_or_remove_items'        => 'Add or remove Items',
        'choose_from_most_used'      => 'Choose from the most used Items',
    );

  register_taxonomy( 'product_tag', 'product', array(
        'labels'                     => $labels,
        'hierarchical'               => true,
  ) );

}
add_action('init', 'wd_hierarchical_tags_register');

添加此代码后,过滤将 "product_tag" 添加到 URL 而不是 "product-tag"。

我该如何解决?

您应该以这种方式在自定义挂钩函数中使用专用过滤器挂钩 woocommerce_taxonomy_args_product_tag

// Customize Woocommerce 'product_tag' custom taxonomy
add_filter( 'woocommerce_taxonomy_args_product_tag', 'custom_product_tag_args', 10, 1 );
function custom_product_tag_args( $args ){
    // Replace with your theme domain name
    $domain = 'woocommerce';

    $permalinks = wc_get_permalink_structure();

    $args['hierarchical'] = true; // <== TRUE
    $args['update_count_callback'] = '_wc_term_recount';
    $args['label'] = __( 'Product tags', $domain );
    $args['labels'] = array(
        'name'                       => __( 'Items', $domain ),
        'singular_name'              => __( 'Item', $domain ),
        'menu_name'                  => _x( 'Item', 'Admin menu name', $domain ),
        'search_items'               => __( 'Search items', $domain ),
        'all_items'                  => __( 'All items', $domain ),
        'parent_item'                => __( 'Parent Item', $domain ),
        'parent_item_colon'          => __( 'Parent Item:', $domain ),
        'edit_item'                  => __( 'Edit item', $domain ),
        'update_item'                => __( 'Update item', $domain ),
        'add_new_item'               => __( 'Add new item', $domain ),
        'new_item_name'              => __( 'New item name', $domain ),
        'popular_items'              => __( 'Popular items', $domain ),
        'separate_items_with_commas' => __( 'Separate items with commas', $domain ),
        'add_or_remove_items'        => __( 'Add or remove items', $domain ),
        'choose_from_most_used'      => __( 'Choose from the most used items', $domain ),
        'not_found'                  => __( 'No items found', $domain ),
    );
    $args['show_ui'] = true;
    $args['query_var'] = true;
    $args['capabilities'] = array(
        'manage_terms' => 'manage_product_terms',
        'edit_terms'   => 'edit_product_terms',
        'delete_terms' => 'delete_product_terms',
        'assign_terms' => 'assign_product_terms',
    );
    $args['rewrite'] = array(
        'slug'       => $permalinks['tag_rewrite_slug'], <== HERE URL PERMALINK
        'with_front' => false,
    );

    return $args;
} 

代码进入活动子主题(或活动主题)的 function.php 文件。

已测试并有效。