Bạn muốn loại bỏ Catagory và slug catagory danh mục sản phẩm trong wordpress?

trước khi code:
ex: https://tamnguyen.vn/danh-muc-a/san-pham-a
ex: https://tamnguyen.vn/danh-muc-a/san-pham-b
ex: https://tamnguyen.vn/sanpham/hoa-yeu-thuong/san-pham-c
sau khi code
ex: https://tamnguyen.vn/san-pham-a
ex: https://tamnguyen.vn/san-pham-b
ex: https://tamnguyen.vn/san-pham-c

Hãy copy đoạn code sau vào file function.php để thực hiện nhé

Code bỏ category và slug category trong wordpress sản phẩm Woocommerce

function cdb_remove_slug( $post_link, $post ) {
if ( !in_array( get_post_type($post), array( ‘product’ ) ) || ‘publish’ != $post->post_status ) {
return $post_link;
}
if(‘product’ == $post->post_type){
$post_link = str_replace( ‘/product/’, ‘/’, $post_link );
}else{
$post_link = str_replace( ‘/’ . $post->post_type . ‘/’, ‘/’, $post_link );
}
return $post_link;
}
add_filter( ‘post_type_link’, ‘cdb_remove_slug’, 10, 2 );
/*Sửa lỗi 404 sau khi đã remove slug product hoặc cua-hang*/
function cdb_woo_product_rewrite_rules($flash = false) {
global $wp_post_types, $wpdb;
$siteLink = esc_url(home_url(‘/’));
foreach ($wp_post_types as $type=>$custom_post) {
if($type == ‘product’){
if ($custom_post->_builtin == false) {
$querystr = “SELECT {$wpdb->posts}.post_name, {$wpdb->posts}.ID
FROM {$wpdb->posts}
WHERE {$wpdb->posts}.post_status = ‘publish’
AND {$wpdb->posts}.post_type = ‘{$type}'”;
$posts = $wpdb->get_results($querystr, OBJECT);
foreach ($posts as $post) {
$current_slug = get_permalink($post->ID);
$base_product = str_replace($siteLink,”,$current_slug);
add_rewrite_rule($base_product.’?$’, “index.php?{$custom_post->query_var}={$post->post_name}”, ‘top’);
}
}
}
}
if ($flash == true)
flush_rewrite_rules(false);
}
add_action(‘init’, ‘cdb_woo_product_rewrite_rules’);
/*Fix lỗi khi tạo sản phẩm mới bị 404*/
function cdb_woo_new_product_post_save($post_id){
global $wp_post_types;
$post_type = get_post_type($post_id);
foreach ($wp_post_types as $type=>$custom_post) {
if ($custom_post->_builtin == false && $type == $post_type) {
cdb_woo_product_rewrite_rules(true);
}
}
}
add_action(‘wp_insert_post’, ‘cdb_woo_new_product_post_save’);
/*xoa duong dan url*/
add_filter( ‘term_link’, ‘cdb_product_cat_permalink’, 10, 3 );
function cdb_product_cat_permalink( $url, $term, $taxonomy ){
switch ($taxonomy):
case ‘product_cat’:
$taxonomy_slug = ‘product-category’; //Thay bằng slug hiện tại của bạn. Mặc định là product-category
if(strpos($url, $taxonomy_slug) === FALSE) break;
$url = str_replace(‘/’ . $taxonomy_slug, ”, $url);
break;
endswitch;
return $url;
}
function cdb_product_category_rewrite_rules($flash = false) {
$terms = get_terms( array(
‘taxonomy’ => ‘product_cat’,
‘post_type’ => ‘product’,
‘hide_empty’ => false,
));
if($terms && !is_wp_error($terms)){
$siteurl = esc_url(home_url(‘/’));
foreach ($terms as $term){
$term_slug = $term->slug;
$baseterm = str_replace($siteurl,”,get_term_link($term->term_id,’product_cat’));
add_rewrite_rule($baseterm.’?$’,’index.php?product_cat=’.$term_slug,’top’);
add_rewrite_rule($baseterm.’page/([0-9]{1,})/?$’, ‘index.php?product_cat=’.$term_slug.’&paged=$matches[1]’,’top’);
add_rewrite_rule($baseterm.'(?: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’, ‘cdb_product_category_rewrite_rules’);
add_action( ‘create_term’, ‘cdb_new_product_cat_edit_success’, 10, 2 );
function cdb_new_product_cat_edit_success( $term_id, $taxonomy ) {
cdb_product_category_rewrite_rules(true);
}

Một số lưu ý trong đoạn code trên

  • /product/ ở dòng số 6 ở code trên, là đường dẫn sản phẩm bạn thay cho đúng với hình bên dưới trong cài đặt của bạn
  • ‘product-category’ ở dòng 54 code ở trên là đường dẫn danh mục sản phẩm, bạn thay cho đúng với trong settings của bạn luôn nhé, thay sai là không hoạt động đâu mà lỗi nữa đó,
  • và thay đúng rồi thì nhớ bước update lại link như bài trên luôn nghen !
  • Lưu lại và xem kết quả nhé.

Code bỏ category và slug category trong wordpress tin tức

Các bạn bỏ đoạn code này vào function.php của theme là xong. nhớ đọc đoạn sau của code này nhé.

// Remove Parent Category from Child Category URL
add_filter(‘term_link’, ‘cdb_no_category_parents’, 1000, 3);
function cdb_no_category_parents($url, $term, $taxonomy) {
if($taxonomy == ‘category’){
$term_nicename = $term->slug;
$url = trailingslashit(get_option( ‘home’ )) . user_trailingslashit( $term_nicename, ‘category’ );
}
return $url;
}
// Rewrite url mới
function cdb_no_category_parents_rewrite_rules($flash = false) {
$terms = get_terms( array(
‘taxonomy’ => ‘category’,
‘post_type’ => ‘post’,
‘hide_empty’ => false,
));
if($terms && !is_wp_error($terms)){
foreach ($terms as $term){
$term_slug = $term->slug;
add_rewrite_rule($term_slug.’/?$’, ‘index.php?category_name=’.$term_slug,’top’);
add_rewrite_rule($term_slug.’/page/([0-9]{1,})/?$’, ‘index.php?category_name=’.$term_slug.’&paged=$matches[1]’,’top’);
add_rewrite_rule($term_slug.’/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$’, ‘index.php?category_name=’.$term_slug.’&feed=$matches[1]’,’top’);
}
}
if ($flash == true)
flush_rewrite_rules(false);
}
add_action(‘init’, ‘cdb_no_category_parents_rewrite_rules’);
/*Sửa lỗi khi tạo mới category bị 404*/
function cdb_new_category_edit_success() {
cdb_no_category_parents_rewrite_rules(true);
}
add_action(‘created_category’,’cdb_new_category_edit_success’);
add_action(‘edited_category’,’cdb_new_category_edit_success’);
add_action(‘delete_category’,’cdb_new_category_edit_success’);

Lưu ý: sau khi bạn add code trên vào function xong thì hay update lại link nhé, không là lỗi 404 đó. nếu bạn không biết thì làm theo đường dẫn dưới nhé

cài đặt -> đường dẫn tĩnh -> lưu lại (tiếng việt)
settings - permalinks - update  ( đối với tiếng anh)

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

0984966806