Skip to content

Hi You, I am Tade

  • About
  • Donate
  • Privacy policy
    • About
    • Donate
    • Privacy policy

Xóa slug trên custom post và custom taxonomy: Phần 1

  • Hoàng Thức
  • Mar 11, 2017

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.

Đầu tiên về custom post (nếu bạn nào chưa biết về custom post có thể xem bài viết trước của mình).

1.Xóa slug trên custom post trong trang single:

Link custom post single ban đầu: https://hoangthuc.com/report/bao-cao-thu-chi/

Link custom post single cần thay thành: https://hoangthuc.com/bao-cao-thu-chi/

Custom post type mình làm ở đây là “report” nếu làm bạn có thể tùy biến custom post này. Các bạn vào file fucntions.php và thêm đoạn code có định dạng bên dưới.

// remove slug custom_post type
function na_remove_slug( $post_link, $post, $leavename ) {

    if ( 'report' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }

    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

    return $post_link;
}
add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );

Note* Trong phần này bạn sẽ dung hàm na_remove_slug để filter vào hook post_type_link

2. Xữ lý lỗi 301 redirect khi chuyển:

Trong quá trình remove slug như bước 1 wp sẽ xuất hiện lỗi 301 không tìm thấy trang. Cách xữ lý.

function na_parse_request( $query ) {

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'report', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'na_parse_request' );

Sau đó các bạn và Setting => Permalink và Save button lại để hoàn tất.

 

Chúc các bạn thành công !

 

    Tags:
  • Custom post wp
  • Thủ thuật wordpress
  • wordpress nâng cao
    Share

Post navigation

←Previous:
Next:→

47 thoughts on “Xóa slug trên custom post và custom taxonomy: Phần 1”

  1. Pingback: ที่พักพัทยา
  2. Pingback: เว็บปั้มไลค์
  3. Pingback: Commercial Air Duct Cleaning in Austin
  4. Pingback: concrete companies near me
  5. Pingback: กระเบื้องยาง SPC ราคา
  6. Pingback: แกนกระดาษ
  7. Pingback: EChome.sg
  8. Pingback: อะไหล่อุตสาหกรรม
  9. Pingback: Browning shotguns
  10. Pingback: Ford Everest
  11. Pingback: APEX esp
  12. Pingback: swan168
  13. Pingback: https://hitoption.net
  14. Pingback: altogel login dan daftar
  15. Pingback: slot wallet คลิกเดียวโบนัสแตก
  16. Pingback: เช่าจอLED
  17. Pingback: jaxx download
  18. Pingback: BAU iraq
  19. Pingback: Superslot ใหม่ล่าสุด
  20. Pingback: Nexus Market
  21. Pingback: ตู้แช่สแตนเลส
  22. Pingback: ATEX
  23. Pingback: ลดข้าวดีด ข้าวเด้ง
  24. Pingback: pgslot
  25. Pingback: Massage
  26. Pingback: lost mary mad blue flavor
  27. Pingback: Nonameauto
  28. Pingback: สมัคร Lsm2play หรือ LSM99PLAY เว็บไหนดีกว่ากัน
  29. Pingback: Go to page
  30. Pingback: Talimatlara git
  31. Pingback: site
  32. Pingback: Aviator
  33. Pingback: จองตั๋วรถทัวร์
  34. Pingback: mostbet yükle
  35. Pingback: 토토 안전한 사이트
  36. Pingback: fun888
  37. Pingback: som777
  38. Pingback: ดูบอลสด ไม่ต้องเสียเงิน
  39. Pingback: เว็บตรงฝากถอนง่าย
  40. Pingback: ที่ดินเขาใหญ่
  41. Pingback: Thai restaurant Portland
  42. Pingback: สายคาดกล่องอาหาร
  43. Pingback: ร้านเค้กวันเกิดใกล้ฉัน
  44. Pingback: endolift
  45. Pingback: dragon vs tiger online game India 2025
  46. Pingback: เครื่องเป่าแอลกอฮอล์
  47. Pingback: exchange xmr without id

Comments are closed.

©2025 Hi You, I am Tade / Designed & Built by WPFound