Cắt URL cate hay sản phẩm trong wordpress tương đương với việc tối ưu link seo, content tốt hỗ trợ seo là tối ưu được url.

Với child theme hay theme gốc đều được.

Câu lệnh cắt url category danh mục không cần dùng Plugin Ultimate Seo.

add_filter( ‘term_link’, ‘devvn_product_cat_permalink’, 10, 3 );
function devvn_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 devvn_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’, ‘devvn_product_category_rewrite_rules’);
add_action( ‘create_term’, ‘devvn_new_product_cat_edit_success’, 10, 2 );
function devvn_new_product_cat_edit_success( $term_id, $taxonomy ) {
devvn_product_category_rewrite_rules(true);
}

Để lại một bình luận

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 *