Last active
November 19, 2022 05:50
-
-
Save levantoan/b3baea54158280e42260eb124f6e6a5f to your computer and use it in GitHub Desktop.
Remove product-category and all parents slug product category in link Woocommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? php | |
// 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 | |
add_filter('rewrite_rules_array', 'devvn_no_product_cat_parents_rewrite_rules'); | |
function devvn_no_product_cat_parents_rewrite_rules($rules) { | |
$new_rules = array(); | |
$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; | |
$new_rules[$term_slug.'/?$'] = 'index.php?product_cat='.$term_slug; | |
$new_rules[$term_slug.'/page/([0-9]{1,})/?$'] = 'index.php?product_cat='.$term_slug.'&paged=$matches[1]'; | |
$new_rules[$term_slug.'/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?product_cat='.$term_slug.'&feed=$matches[1]'; | |
} | |
} | |
return $new_rules + $rules; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*************************** | |
* Loại bỏ toàn bộ slug của category cha và product-category ra khỏi URL | |
* Remove all parents slug product category link | |
* Author: http://levantoan.com | |
* Example | |
Product category url: http://domain.com/product-category/computer | |
Product child category url level 1: http://domain.com/product-category/computer/laptop | |
Product child category url level 2: http://domain.com/product-category/computer/laptop/dell | |
* Change to | |
Product category url: http://domain.com/computer | |
Product child category url level 1: http://domain.com/laptop | |
Product child category url level 2: http://domain.com/dell | |
* Code | |
Read more here: http://levantoan.com/xoa-bo-product-category-va-toan-bo-slug-cua-danh-muc-cha-khoi-duong-dan-cua-woocommerce/ | |
***************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
im interested in the same ^