Skip to content

Hi You, I am Tade

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

Tạo template trong plugin wordpress

  • Hoàng Thức
  • Jul 26, 2017

Thông thường để tạo template cho trang bạn phải vào theme để tạo template. Nhưng vấn đề ở đây là khi bạn muốn thay đổi theme thì template vẫn không thay đổi. Giải pháp đưa ra là bạn tạo template trong plugin để tách ra khỏi theme. Như vầy dù bạn có thay đổi theme thì template vẫn giữ nguyên. Tiêu biễu như plugin Woocommerce

Các loại template các bạn dùng custom post, archive và page.

Các hook được sử dụng: single_template, archive_template và page_template

Tiến hành code.

1.Custom post

Đối với custom post ta dung hook single_template. ‘ YOUR_CUSTOM_POST ‘ là tên custom post của bạn dùng. 

function get_custom_post_type_single($single_template) {
    global $post;
    if ($post->post_type == 'YOUR_CUSTOM_POST') {
        $single_template = dirname( __FILE__ ) . '/single-YOUR_CUSTOM_POST.php';
    }
    return $single_template;
}
add_filter( 'single_template', 'get_custom_post_type_single' );

2.Archive

Đối với trang archive ta dung hook archive_template. ‘ YOUR_TAXONOMY ‘ là tên taxonomy của bạn dùng.

function get_custom_archive($archive_template){
    global $wpdb;
    $archive =$wpdb->last_result;
    if ( $archive[0]->taxonomy==  'YOUR_TAXONOMY' ) {
        $archive_template = dirname( __FILE__ ) . '/archive-YOUR_TAXONOMY.php';
    }
    return $archive_template;
}
add_filter( "archive_template", "get_custom_archive" ) ;

3.Page Template

Đối với page bạn phải chọn slug chính xác thì template mới được sử dụng.

add_filter( 'page_template', 'wpa_page_template' );
function wpa_page_template( $page_template )
{
if ( is_page( YOUR_SLUG ) ) {
$page_template = dirname( __FILE__ ) . '/templates/template-YOUR_SLUG.php';
}
return $page_template;
}

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

    Tags:
  • plugin wordpress
  • Taxonomy wordpress
  • Thủ thuật wordpress
  • wordpress nâng cao
    Share

Post navigation

←Previous:
Next:→

61 thoughts on “Tạo template trong plugin wordpress”

  1. Pingback: UWIN789
  2. Pingback: barber Melbourne
  3. Pingback: xxxโอลี่แฟน
  4. Pingback: כתום אחסון אתרים
  5. Pingback: pilsakmens
  6. Pingback: login dultogel
  7. Pingback: เหล้านอก
  8. Pingback: skool games alex hormozi
  9. Pingback: เช็คสถิติหวย
  10. Pingback: Browning firearms for sale near me
  11. Pingback: Ford Everest
  12. Pingback: dultogel 777
  13. Pingback: นามบัตรด่วน
  14. Pingback: PBT
  15. Pingback: learn the facts here now
  16. Pingback: ทำจมูก พัทยา
  17. Pingback: ที่ปรึกษาขออย
  18. Pingback: bobs-seo.com/las-vegas/
  19. Pingback: Scuba diving koh tao
  20. Pingback: jeeters disposable price
  21. Pingback: eft cheats
  22. Pingback: thailand tattoo
  23. Pingback: ลดข้าวดีด ข้าวเด้ง
  24. Pingback: ทำความรู้จักกับ เว็บ บาคาร่า วอเลท
  25. Pingback: som777
  26. Pingback: kc9
  27. Pingback: pgslot168
  28. Pingback: สกรีนแก้ว
  29. Pingback: เช่ารถตู้พร้อมคนขับ
  30. Pingback: couples massage
  31. Pingback: ตั๋วรถทัวร์
  32. Pingback: 789bet
  33. Pingback: ซักผ้าอุตสาหกรรม
  34. Pingback: lotto888
  35. Pingback: แอร์โซล่าเซลล์
  36. Pingback: Games Global ค่ายสล็อตเปิดใหม่
  37. Pingback: ขายอะไรดี
  38. Pingback: ปั้มไลค์
  39. Pingback: Dorothy
  40. Pingback: โรงงานผลิตสปริง
  41. Pingback: ร้านดอกไม้
  42. Pingback: essentials fear of god
  43. Pingback: rich89bet
  44. Pingback: romanviagra.com
  45. Pingback: เกม ใหม่ pg
  46. Pingback: buy caverta 100mg
  47. Pingback: bystolic medicine
  48. Pingback: Sweettooth
  49. Pingback: cialis low prices cheap
  50. Pingback: kamagra kaufen
  51. Pingback: whats a viagra pill
  52. Pingback: EndoliftX
  53. Pingback: levitinfo.wordpress.com
  54. Pingback: blackcial.wordpress.com
  55. Pingback: onglyza.wordpress.com
  56. Pingback: ventolin inhaler recall
  57. Pingback: sketchfab.com/cialis-daily
  58. Pingback: dorzolamide hcl timolol maleate for dogs
  59. Pingback: vigrakrs.com
  60. Pingback: androgel testosterone
  61. Pingback: cipmox 500 tablet

Comments are closed.

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