Có thể RankMath đã bỏ quên hoặc không muốn sử dụng chức năng này, họ chỉ có cấu hình xoá tên đường dẫn /category/ mà không hề rút gọn tên chuyên mục cấp 2
Mặc định:
https://wordpressvn.com/category/chuyen-muc/
Chức năng Strip Category Base sẽ chuyển thành
https://wordpressvn.com/chuyen-muc-cha/chuyen-muc-con/
Sau khi sử dụng đoạn code bên dưới sẽ trở thành
https://wordpressvn.com/chuyen-muc-1-cap/
Các bạn cần tạo thêm file rank-math.php hoặc chèn trực tiếp vào functions.php trong theme child nội dung như sau:
<?php
/**
* Remove parent slugs from category post URL
*/
add_filter('term_link', function ($link, $term, $taxonomy) {
if ( $taxonomy !== 'category' ) {
return $link;
}
$category_base = get_option( 'category_base' );
if ( empty( $category_base ) ) {
$category_base = 'category';
}
if ( '/' === substr( $category_base, 0, 1 ) ) {
$category_base = substr( $category_base, 1 );
}
$category_base .= '/';
if (RankMath\Helper::get_settings( 'general.strip_category_base' ) == true) {
$link = home_url( user_trailingslashit( $category_base . $term->slug ) );
}
return preg_replace( '`' . preg_quote( $category_base, '`' ) . '`u', '', $link, 1 );
}, 10, 3);
add_filter( 'rewrite_rules_array', function( $rules) {
global $wp_rewrite;
wp_cache_flush();
if (RankMath\Helper::get_settings( 'general.strip_category_base' ) == true) {
$category_rewrite = array();
$category_rewrite['(.+?)-cat/?$'] = 'index.php?category_name=$matches[1]';
$category_rewrite['(.+?)-cat/'.$wp_rewrite->pagination_base.'/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
$categories = get_categories(array('hide_empty'=>false));
if ($categories)
{
foreach ($categories as $key => $val)
{
$posts = get_posts (array("name" => $val->slug));
if (!$posts)
{
$category_rewrite['('.$val->category_nicename.')/?$'] = 'index.php?category_name=$matches[1]';
$category_rewrite['('.$val->category_nicename.')/'.$wp_rewrite->pagination_base.'/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
$category_rewrite['.+?/('.$val->category_nicename.')/?$'] = 'index.php?category_name=$matches[1]';
$category_rewrite['.+?/('.$val->category_nicename.')/'.$wp_rewrite->pagination_base.'/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
}
}
}
return $category_rewrite + $rules;
}
});
Nếu bạn thấy hay hãy Donate hoặc click vào các link affilite để ủng hộ Diễn đàn nhé.
Vui lòng để nguồn tác giả: WPVNTEAM tại WordPressVN