Thêm đoạn code sau vào functions.php
// Xóa /category/ khỏi URL
function remove_category_base($string, $term) {
if ($term->taxonomy == 'category') {
$string = str_replace('/category/', '/', $string);
}
return $string;
}
add_filter('term_link', 'remove_category_base', 10, 2);
// Thêm rewrite rules để tránh lỗi 404
function add_category_rewrite_rules($wp_rewrite) {
$rules = [];
$categories = get_categories(['hide_empty' => false]);
foreach ($categories as $category) {
$slug = $category->slug;
$rules['(' . $slug . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=' . $slug . '&feed=$matches[2]';
$rules['(' . $slug . ')/page/([0-9]{1,})/?$'] = 'index.php?category_name=' . $slug . '&paged=$matches[2]';
$rules['(' . $slug . ')/?$'] = 'index.php?category_name=' . $slug;
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'add_category_rewrite_rules');
// Redirect URL cũ chứa /category/ sang URL mới
function redirect_old_category_links() {
if (strpos($_SERVER['REQUEST_URI'], '/category/') !== false) {
$new_url = str_replace('/category/', '/', $_SERVER['REQUEST_URI']);
wp_redirect(home_url($new_url), 301);
exit();
}
}
add_action('template_redirect', 'redirect_old_category_links');
Làm mới cấu trúc Permalink
Sau khi thêm code vào file functions.php, bạn cần cập nhật lại cấu trúc đường dẫn tĩnh:
- Vào Settings (Cài đặt) > Permalinks (Đường dẫn tĩnh).
- Nhấn nút Save Changes (Lưu thay đổi) mà không cần chỉnh sửa gì.
- Việc này buộc WordPress làm mới lại cấu trúc URL.
Chào ! Bạn thấy nội dung này thế nào?





