+1

Hiểu rõ về bản chất Custom Post Type trong WordPress

Xin chào các bạn. Sau bài viết về cách tạo một widget trong WordPress. Hôm nay mình sẽ viết về cách để tự tạo một Custom Post Type trong WordPress. Mình sẽ nói về những tham số trong Custom Post Type trong WordPress

Custom Post Type trong WordPress là gì?

Với các bạn đã sử dụng WordPress đều biết Post và Page trong WordPress đều là Post Type. Đó là mặc định của WordPress. Nhưng với các bạn muốn tạo ra 1 page để quản lý tin tức hay quản lí sản phẩm trong WordPress thì khi đó các bạn sẽ đụng tới Custom Post Type Custom Post Type sẽ kế thừa toàn bộ chức năng của Post, bạn có thể thêm category, feature image, meta options, custom field.....và show Post Type ra front page

  • Vấn đề 1 : Code mẫu về tạo ra Custom Post Type trong WordPress
function create(){
		
		$labels = array(
                'name' 				=> __('Products'),
                'singular_name' 	=> __('Product'),
                'menu_name'			=> __('ZProduct'),
                'name_admin_bar' 	=> __('ZProduct'),
                'add_new'			=> __('Add Product'),
                'add_new_item'		=> __('Add New Product'),
                'search_items' 		=> __('Search Product'),
                'not_found'			=> __('No products found.'),
                'not_found_in_trash'=> __('No products found in Trash'),
                'view_item' 		=> __('View product'),
                'edit_item'			=> __('Edit product'),
				);
		$args = array(
				'labels'               => $labels,
				'description'          => 'Hiển thị nội dung mô tả về phần Custom Post',
				'public'               => true,
 				'hierarchical'         => true,
                // 'exclude_from_search'  => null, //public
                // 'publicly_queryable'   => null, //public
                // 'show_ui'              => null, //public
                // 'show_in_menu'         => null, 
 				'show_in_nav_menus'    => true, //public
 				'show_in_admin_bar'    => true, //public
 				'menu_position'        => 5,
 				'menu_icon'            => ZENDVN_MP_IMAGES_URL . '/icon-setting16x16.png',
 				'capability_type'      => 'post',
                // 'capabilities'         => array(),
                // 'map_meta_cap'         => null,
 				'supports'             => array('title' ,'editor','author','thumbnail','excerpt','trackbacks' ,'custom-fields' ,'comments','revisions' ,'page-attributes','post-formats'),
                 // 'register_meta_box_cb' => null,
                //	'taxonomies'           => array(),
 				'has_archive'          => true,
 				'rewrite'              => array('slug'=>'zproduct'),
                // 'query_var'            => true,
                // 'can_export'           => true,
                // 'delete_with_user'     => null,
               // '_builtin'             => false,
 				'_edit_link'           => 'post.php?post=%d',
		);
		
	}
  • Vấn đề 2 : Mình sẽ giải thích các tham số Custom Post Type trong WordPress

name : Đó chính tên của Custom Post Type nằm bên tay trái của page WordPress menu_name: Đó chính là tên Custom Post Type nằm bên tay trái của page WordPress , nếu mình không có truyền menu_name thì WordPress nó sẽ lấy name làm menu_name name_admin_bar : Đó chính tên menu nó nằm trên Menu Top WordPress khi bạn click New trên thì các bạn sẽ thấy nó
add_new : Đó chính tên menu khi bạn click Product thì sẽ có "'Add Product" add_new_item : Đó chính tên cái tiêu đề "'Add New Product " khi bạn ckick vào thêm 1 Producct mới ngay trên cái tiêu đề của sản phẩm bạn muốn thêm not_found: Đó chính là khi bạn tìm kiếm sản phẩm không có như giá trị bạn nhập vào thì nó sẽ hiển thị câu thông báo là "No products found" description : Mô tả về Post type của Product bạn đã tạo ra public: có 2 giá trị true or false , mặc định mình không khai báo thì nó sẽ tự hiểu là true , còn nếu mình khai báo là false thì nó Custom Post Type Product không hiển thị bên menu trái WordPress

Bắt đầu tạo Custom Post Type Product

  • Đầu tiền mình mở file function.php ra để bỏ đoạn code vào dưới đây vào
add_action ( 'init', 'create' );
function create(){
   	
   	$labels = array(
               'name' 				=> __('Products'),
               'singular_name' 	=> __('Product'),
               'menu_name'			=> __('ZProduct'),
               'name_admin_bar' 	=> __('ZProduct'),
               'add_new'			=> __('Add Product'),
               'add_new_item'		=> __('Add New Product'),
               'search_items' 		=> __('Search Product'),
               'not_found'			=> __('No products found.'),
               'not_found_in_trash'=> __('No products found in Trash'),
               'view_item' 		=> __('View product'),
               'edit_item'			=> __('Edit product'),
   			);
   	$args = array(
   			'labels'               => $labels,
   			'description'          => 'Hiển thị nội dung mô tả về phần Custom Post',
   			'public'               => true,
				'hierarchical'         => true,
               // 'exclude_from_search'  => null, //public
               // 'publicly_queryable'   => null, //public
               // 'show_ui'              => null, //public
               // 'show_in_menu'         => null, 
				'show_in_nav_menus'    => true, //public
				'show_in_admin_bar'    => true, //public
				'menu_position'        => 5,
				'menu_icon'            => ZENDVN_MP_IMAGES_URL . '/icon-setting16x16.png',
				'capability_type'      => 'post',
               // 'capabilities'         => array(),
               // 'map_meta_cap'         => null,
				'supports'             => array('title' ,'editor','author','thumbnail','excerpt','trackbacks' ,'custom-fields' ,'comments','revisions' ,'page-attributes','post-formats'),
                // 'register_meta_box_cb' => null,
               //	'taxonomies'           => array(),
				'has_archive'          => true,
				'rewrite'              => array('slug'=>'zproduct'),
               // 'query_var'            => true,
               // 'can_export'           => true,
               // 'delete_with_user'     => null,
              // '_builtin'             => false,
				'_edit_link'           => 'post.php?post=%d',
   	);
   	
   }

Lời kết :

Sau này bài này của mình , mình hi vọng các bạn hiểu rõ phần nào về Custom Post Type trong WordPress , tại sao mình giải thích kĩ cho các bạn rõ về Custom Post Type vì mình biết có nhiều bạn không hiểu rõ về các tham số không hiểu rõ về các tham số mà WordPress cung cấp . Bài viết sau mình sẽ hướng dẫn rõ các bạn về tạo 1 plugin và tạo ra Custom Post Type Custom Post Type trong WordPress mà không không viết theo dạng function mà mình sẽ hướng dẫn theo OOP ( viết theo dạng Class )


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí