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

时间:2018-01-14 19:58:45

标签: php wordpress woocommerce permalinks custom-taxonomy

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

var userDic = new Dictionary<string, string> {
        {"399969178745962506", "One"},
        {"104729417217032192", "Two"}
    };
var p =  @"<@!?(\d+)>";
var s = "<@399969178745962506> hello to <@!104729417217032192>";
Console.WriteLine(
    Regex.Replace(s, p, m => userDic.ContainsKey(m.Groups[1].Value) ?
        userDic[m.Groups[1].Value] : m.Value
    )
); // => One hello to Two
// Or, if you need to keep <@, <@! and >
Console.WriteLine(
    Regex.Replace(s, @"(<@!?)(\d+)>", m => userDic.ContainsKey(m.Groups[2].Value) ?
        $"{m.Groups[1].Value}{userDic[m.Groups[2].Value]}>" : m.Value
    )
); // => <@One> hello to <@!Two>

添加此代码后,过滤会将“product_tag”添加到网址而不是“product-tag”。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您应该以这种方式在自定义挂钩函数中使用专用过滤器挂钩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文件。

经过测试和工作。