Mặc định chúng ta có
Danh mục sản phẩm: https://dominhnhut.com/product-category/may-tinh
Danh mục con cấp 1: https://dominhnhut.com/product-category/may-tinh/may-tinh-de-ban
Danh mục con cấp 2: https://dominhnhut.com/product-category/may-tinh/may-tinh-de-ban/may-tinh-dell
Sau khi áp dụng đoạn code này chúng ta có kết quả như sau:
Danh mục sản phẩm: https://dominhnhut.com/may-tinh
Danh mục con cấp 1: https://dominhnhut.com/may-tinh-de-ban
Danh mục con cấp 2: https://dominhnhut.com/may-tinh-dell
Để sử đụng bạn copy đoạn code sau vào file functions.php trong thư mục theme bạn đang sử dụng nhé!
// Remove product cat base add_filter('term_link', 'devvn_no_term_parents', 1000, 3); function devvn_no_term_parents($url, $term, $taxonomy) { if($taxonomy == 'product_cat'){ $term_nicename = $term->slug; $url = trailingslashit(get_option( 'home' )) . user_trailingslashit( $term_nicename, 'category' ); } return $url; } // Add our custom product cat rewrite rules function devvn_no_product_cat_parents_rewrite_rules($flash = false) { $terms = get_terms( array( 'taxonomy' => 'product_cat', 'post_type' => 'product', 'hide_empty' => false, )); if($terms && !is_wp_error($terms)){ foreach ($terms as $term){ $term_slug = $term->slug; add_rewrite_rule($term_slug.'/?$', 'index.php?product_cat='.$term_slug,'top'); add_rewrite_rule($term_slug.'/page/([0-9]{1,})/?$', 'index.php?product_cat='.$term_slug.'&paged=$matches[1]','top'); add_rewrite_rule($term_slug.'/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat='.$term_slug.'&feed=$matches[1]','top'); } } if ($flash == true) flush_rewrite_rules(false); } add_action('init', 'devvn_no_product_cat_parents_rewrite_rules'); /*Sửa lỗi khi tạo mới taxomony bị 404*/ add_action( 'create_term', 'devvn_new_product_cat_edit_success', 10); add_action( 'edit_terms', 'devvn_new_product_cat_edit_success', 10); add_action( 'delete_term', 'devvn_new_product_cat_edit_success', 10); function devvn_new_product_cat_edit_success( ) { devvn_no_product_cat_parents_rewrite_rules(true); }
Chúc các bạn thành công!