Skip to content

Hi You, I am Tade

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

WordPress nâng cao 1: Custom post type

  • Hoàng Thức
  • May 9, 2016

Trong wordpress có 2 loại custom post đã được mặc định sẳn là post và page. Post cho bài viết và page cho trang .Vậy nếu muốn tạo thêm 1 custom post ta làm thế nào. Bài viết sau sẽ hướng dẫn các bạn tạo 1 custom post đợn giản nhất.

Trước hết mình xin giới thiệu cách tạo custom post đơn giản nhất có thể dành cho người không chuyên về code đó là dùng plugin. Các plugin cho phép bạn tạo mọi thứ về custom post như taxonomy, field , post type,…

Custom post type UI

custom-post-type-UI

Type

types

Đây là 2 plugin mình đã sử dụng qua và theo đánh giá của mình thì rất đầy đủ.

Và cách thứ 2 mình muốn hướng dẫn đó là code. Các bạn có thể tích hợp trong plugin, hoặc theme.

Khởi tạo custom post type

<?php
 if( ! function_exists( 'Domain_create_post_type' ) ) :
 function Domain_create_post_type() {
 $labels = array(
 'name' => 'Domain',
 'singular_name' => 'Domain',
 'add_new' => 'Add Domain',
 'all_items' => 'All Domain',
 'add_new_item' => 'Add Domain',
 'edit_item' => 'Edit Domain',
 'new_item' => 'New Domain',
 'view_item' => 'View Domain',
 'search_items' => 'Search Domain',
 'not_found' => 'No Domain found',
 'not_found_in_trash' => 'No Domain found in trash',
 'parent_item_colon' => 'Parent Domain'
 //'menu_name' => default to 'name'
 );
 $args = array(
 'labels' => $labels,
 'public' => true,
 'has_archive' => true,
 'publicly_queryable' => true,
 'query_var' => true,
 'rewrite' => true,
 'capability_type' => 'post',
 'hierarchical' => false,
 'supports' => array(
 'title',
 'editor',
 ),
 'menu_position' => 5,
 'menu_icon' => 'dashicons-clipboard',
 'exclude_from_search' => false,
 'register_meta_box_cb' => 'domain_add_post_type_metabox'
 );
 register_post_type( 'domain', $args );

register_taxonomy( 'domain_category', // register custom taxonomy - category
 'domain',
 array(
 'hierarchical' => true,
 'labels' => array(
 'name' => 'Domain category',
 'singular_name' => 'Domain category',
 )
 )
 );
 register_taxonomy( 'domain_tag', // register custom taxonomy - tag
 'domain',
 array(
 'hierarchical' => false,
 'labels' => array(
 'name' => 'Domain tag',
 'singular_name' => 'Domain tag',
 )
 )
 );
 }
 add_action( 'init', 'Domain_create_post_type' );
endif;

 function domain_add_post_type_metabox() { 
 add_meta_box( 'domain_metabox', 'Information', 'domain_metabox', 'domain', 'normal' );
 }


 function domain_metabox() {
 global $post;
 $registrar = get_post_meta($post->ID, 'registrar', true);

echo '<div class="form-horizontal" > 
<div class="form-group"> 
<label for="inputEmail3" class="col-sm-2 control-label">Registrar</label> 
<div class="col-sm-10"> <input type="text" class="form-control" name="registrar" value="'.$registrar .'" > 
</div> </div>
</div>'; 

function domain_post_save_meta( $post_id, $post ) { 
 $domain_post_meta['registrar'] = strtotime($_POST['registrar']);
 foreach( $domain_post_meta as $key => $value ) { 
 $value = implode(',', (array)$value); 
 if( get_post_meta( $post->ID, $key, FALSE ) ) { 
 update_post_meta($post->ID, $key, $value);
 } else { 
 add_post_meta( $post->ID, $key, $value );
 }
 if( !$value ) { 
 delete_post_meta( $post->ID, $key );
 }
 }
 }
 add_action( 'save_post', 'domain_post_save_meta', 1, 2 ); 
?>

Trong đây mình có làm phần tích hợp meta box nếu các bạn thấy không cần thiết thì bỏ ra nhé.

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

 

    Tags:
  • wordpress nâng cao
    Share

Post navigation

←Previous:
Next:→

51 thoughts on “WordPress nâng cao 1: Custom post type”

  1. Pingback: Plugin furniture images tạo album, slideshow theo danh mục, tag - HoangThuc
  2. Pingback: plumber woodstock ga
  3. Pingback: ปั้มไลค์
  4. Pingback: pgroyalbet
  5. Pingback: รับออกแบบร้าน
  6. Pingback: junk listings
  7. Pingback: บริษัทรับทำเว็บไซต์
  8. Pingback: virtual office for rent
  9. Pingback: APEX aimbot
  10. Pingback: Digital Marketing Agency
  11. Pingback: phim tinh cam
  12. Pingback: สล็อตเว็บตรง
  13. Pingback: รวมเกมไพ่ออนไลน์ทุกรูปแบบ ทุนน้อยก็เล่นได้
  14. Pingback: ไม้พื้น
  15. Pingback: ทัวร์ธุรกิจจีน
  16. Pingback: สอนภาษาไทยให้ชาวต่างชาติ
  17. Pingback: เรียนสัก
  18. Pingback: เช่ารถตู้พร้อมคนขับ
  19. Pingback: ฟิลเลอร์
  20. Pingback: ของเล่นสนาม
  21. Pingback: ราชา lotto มีจุดเด่นอย่างไร
  22. Pingback: ดูบอลสด66
  23. Pingback: Read more
  24. Pingback: LOTTO432
  25. Pingback: n-ethylpentedrone kopen | buy 2mmc | 6 apb pellets | buy 5-mapb | deschloroketamine | 4-mpd (4-methylpentedrone) | 6 apb powder | 2-mmc pellets, 5-mapb | 2-mmc crystalline powder | 4bmc poeder | acheter 3-me-pcp | buy cathinonen | buy 6 apb powder |NEP N-
  26. Pingback: Angthong National Marine Park
  27. Pingback: Plinko
  28. Pingback: navigate to these guys
  29. Pingback: y2k168
  30. Pingback: กระดาษสติ๊กเกอร์ความร้อน
  31. Pingback: Ricky Casino
  32. Pingback: บทสรุป ทางเข้า Lotto
  33. Pingback: power supply for gaming
  34. Pingback: นวดหน้ายกกระชับ
  35. Pingback: เว็บตรงฝากถอนง่าย
  36. Pingback: albyterol.com
  37. Pingback: tolvaptan and adpkd
  38. Pingback: iverscab 6 mg
  39. Pingback: ออกแบบโรงแรม
  40. Pingback: pills similar to viagra
  41. Pingback: viagra for men cvs
  42. Pingback: heng678
  43. Pingback: Spribe Official Website
  44. Pingback: 5 izgalmas utvonal magyarorszagon amit latnod kell
  45. Pingback: cialis walgreens price
  46. Pingback: health20253.wordpress.com
  47. Pingback: viasuper.wordpress.com
  48. Pingback: Ufavision เว็บแทงบอลออนไลน์
  49. Pingback: stromecinfo.wordpress.com
  50. Pingback: zithrom.wordpress.com
  51. Pingback: super p force

Comments are closed.

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