Nhằm giúp các bạn tối ưu đường dẫn 1 cách gọn nhất khi tạo các custom post mới. Mình có làm 1 tus cách xóa slug cho trang single và category khi tạo custom post, mà wordpress chỉ hổ trợ cho 2 loại là post và page.
Phần 2 mình sẽ hướng dẫn về cách ẩn slug trên taxonomy custom post khi show bài viết. Các bạn nào chưa xem phần 1 hướng dẫn về cách ẩn slug trên custom post.
Phần này mình sẽ hướng dẫn theo 2 cách , thứ nhất là dung hook trong code wordpress, hai là dùng plugin wordpress. Cách 2 có thể dùng dễ dàng cho các bạn không rành về code wordpress.
1.Dùng hook request để ẩn slug taxonomy
add_filter('request', 'rudr_change_term_request', 1, 1 );
function rudr_change_term_request($query){
$tax_name = 'report_category';
if( $query['attachment'] ) :
$include_children = true;
$name = $query['attachment'];
else:
$include_children = false;
$name = $query['name'];
endif;
$term = get_term_by('slug', $name, $tax_name);
if (isset($name) && $term && !is_wp_error($term)):
if( $include_children ) {
unset($query['attachment']);
$parent = $term->parent;
while( $parent ) {
$parent_term = get_term( $parent, $tax_name);
$name = $parent_term->slug . '/' . $name;
$parent = $parent_term->parent;
}
} else {
unset($query['name']);
}
switch( $tax_name ):
case 'category':{
$query['category_name'] = $name; // for categories
break;
}
case 'post_tag':{
$query['tag'] = $name; // for post tags
break;
}
default:{
$query[$tax_name] = $name; // for another taxonomies
break;
}
endswitch;
endif;
return $query;
}
Note* Trong phần này mình dùng hàm rudr_change_term_request để filter hook request
2. Dùng plugin wp htaccess control
Các bạn vào setting >> htaccess control

Chọn Remove Taxonomies and author base sau đó tắt các slug taxonomy mà bạn muốn.

Sau đó save lại. nếu vẫn chưa hoạt động các bạn vào setting >> permalink bấm save lại.
Chúc các bạn thành công !
49 thoughts on “Xóa slug trên custom post và custom taxonomy: Phần 2”
Comments are closed.