Custom Post Type trong Wordpress
Một điểm rất hay ở Wordpress 3.0 là khả năng tạo được các post type riêng (việc mà trước đây phải làm vất vả hơn)
. Bạn chỉ cần thêm đoạn mã sau vào functions.php:
function post_type_huongdan() {
register_post_type( 'huongdan',
array( 'label' => __('Hướng dẫn'), 'public' => true, 'show_ui' => true ) );
register_taxonomy_for_object_type('post_tag', 'huongdan');
}
add_action('init', 'post_type_huongdan');

Sử dụng thêm tham số
Bạn có thể khai báo thêm các thông số như excerpts, custom fields, comment... cho post type của mình như sau:
function post_type_huongdan() {
register_post_type(
'huongdan',
array('label' => __('Hướng dẫn'),
'public' => true,
'show_ui' => true,
'supports' => array(
'title',
'excerpt',
'author',
'trackbacks',
'custom-fields',
'comments',
'revisions')
)
);
register_taxonomy_for_object_type('post_tag', 'huongdan');
}
add_action('init', 'post_type_huongdan');

Bạn có thể xem thêm danh sách các thông số support tại add_post_type_support() document.
Thêm Custom Taxonomies
Tiếp theo ta sẽ thêm taxonomy cho type "Hướng dẫn": category và tag riêng. Category là taxonomy có thứ tự (hierarchies) và Tag không có thứ tự. Bạn sửa hàm post_type_huongdan() như sau:
function post_type_huongdan() {
register_post_type(
'huongdan',
array('label' => __('Hướng dẫn'),
'public' => true,
'show_ui' => true,
'supports' => array(
'title',
'excerpt',
'author',
'trackbacks',
'custom-fields',
'comments',
'revisions')
)
);
register_taxonomy( 'category', 'huongdan', array( 'hierarchical' => true, 'label' => __('Category') ) );
register_taxonomy( 'tag', 'huongdan',
array(
'hierarchical' => false,
'label' => __('Tags'),
'query_var' => 'tag',
'rewrite' => array('slug' => 'tag' )
)
);
}

Bạn có thể xem chi tiết hơn về wordpress post type tại đây

. Hiện mình đang sống tại Hà Nội. Sở thích: làm website và giúp đỡ mọi người phát triển website theo chiều hướng tốt đẹp hơn.