27. Sử dụng WordPress Custom Post Type căn bản

Sau khi cài đặt WordPress thìmặcđịnhhệthốngsẽcungcấpchobạn 2 kiểuđịnhdạngbàiviếtlà Post & Page, tuynhiênnếunhucầucủabạnmuốnsửdụngnhiềukiểu Posts, vídụnhưmuốnđưalên website mộtsảnphẩmnàođóthìbắtbuộcbạnphảitạomộtkiểuđịnhdạngmới (Post Type) tươngứngvớinhữnggìmàbạnđangmongmuốn ,vàcáikiểuđịnhdạngmớinày hay cònđượcgọilà Custom Post Type. Vớikiểuđịnhdạngmớithìbạnhoàntoànsửdụnglạiđượcnhữngtínhnăngnhưlà category, tags, featured images. Đâylàmộtphầnkhálàquantrọngđốivớimột Developer WordPress, trongbàinàytôisẽhướngdẫnbạn Sửdụng WordPress Custom Post Type cănbản.

1/ Cấutrúc plugin kenshin post

Việctạo ra cáckiểu Custom Post type rấtlàđơngiảnvìcómộtsốcôngcụgiúpbạnkhôngcầnviết code vẫntạo ra đượccáckiểu Post mớimộtcáchnhanhchóng. Tuynhiêntrongbàitôisẽkhônghướngdẫnbạntheocáchđó, vìđúngvớitiêuchímà Series đề ra làbạnsẽtừngbướclậptrìnhvàgõtừngdòng code đểtạo ra nhữngthứliênquantớihệthống WordPress.

Tôisẽtạomớimột plugin nhưsau.

Cấutrúc plugin

kenshin-post – images – posts – includes – kenshin-post.php (File xửlýchínhcủa plugin)

Nội dung file kenshin-post.php:

<?php
/**
 * @package Kenshin Custom Post
* @version 0.1
*/
/*
Plugin Name: Kenshin Custom Post
Plugin URI: http://laptrinhweb.org
Description: Đâylàmột plugin dùngđểminhhọa Custom Post type.
Author: Kenshin
Version: 0.1
Author URI: None
*/
define('KENSHIN_POST_PLUGIN_URL', plugin_dir_url(__FILE__));
define('KENSHIN_POST_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('KENSHIN_POST_INCLUDES_DIR', KENSHIN_POST_PLUGIN_DIR .'/includes');
define('KENSHIN_POST_IMAGES_URL', KENSHIN_POST_PLUGIN_URL . 'images');
define('KENSHIN_POST_POSTS_DIR', KENSHIN_POST_PLUGIN_DIR . '/posts');
if(!is_admin()){
    require_once KENSHIN_POST_INCLUDES_DIR. '/frontend.php';
    new KenshinFrontend();
}else{
    require_once KENSHIN_POST_INCLUDES_DIR. '/backend.php';
    new KenshinBackend();
}
require_once KENSHIN_POST_POSTS_DIR. '/product.php';
new Kenshin_Custom_Post_Product();

Tôisẽtạomớimộtkiểuđịnhdạngtươngtựnhưkiểu Post nhưngphần slug củanósẽcótênlà kproduct.

Sử dụng WordPress Custom Post Type căn bản

cau-truc-plugin-kenshin-post

2/ Phươngthứcregister_post_type()

Đểcóthểkhaibáomột Custom Post Type thìtrướctiênbạncầnphảitìmhiểuphươngthức register_post_type() đâylàphươngthứcdùngđểđăngkývànạp Custom Post Type màbạnmuốntạovàohệthống WordPress.

Phươngthứccótấtcảlàhaithamsố, thamsốđầutiênlàtên slug vàthamsốthứhailàmộtmảngchứacáckhaibáo lien quantới Custom Post.

Cúpháp:

1

register_post_type('slug',$args);

Thamsố:

· $slug: Tên slug màbạnđặtđểxácđịnhkiểuđịnhdạng post.

· $args: Mảngchứacácthông tin khaibáoliênquantới Custom Post.

Kếtiếptôisẽviết code xửlý ở file product.php.

<?php
class Kenshin_Custom_Post_Product{
    public function __construct(){
        add_action('init', array($this, 'create'));
    }
    public function create(){
        $args = array(); // Cácthamsốquantrọngđốivới Custom Post
        register_post_type('kproduct', $args); // Phươngthứcđăngký&nạp custom post vàohệthống.
    }
}

Đầutiênlàtạophươngthức create() dùngđểtạo Custom Post vàdùng Hook Init kíchhoạtphươngthứcnạpvàohệthống. thựchiệnxongviệcnàybạnvàovùng admin vàbấm F5 thìvẫnchưathấygìthayđổiđâu.

Vìlúcnàybạnvẫnchưatruyềncácthamsốvàomảng $args thìlàmsaonócódữliệumàhiểnthị ra chứ. Nhưngbạncầnphảivàođường dẫn http://codex.wordpress.org/Function_Reference/register_post_type đểtìmhiểuvềcácthamsốquantrọnglàmnênmột Custom Post.

3/ Cácthamsốdùngđểtạo Custom Post Type

Trướctiêntôisẽkhaibáotên Post Type dạngsốnhiềuvàsốítvớihaithamsốđầutiênlà name &singular_name vàsửdụngthamsố menu_icon đểthayđổibiểutượng menu luôn.

class Kenshin_Custom_Post_Product {
  public function __construct() {
    add_action('init', array($this, 'create'));
  }
  public function create() {
    $labels = array(
      'name'          => 'Products',
      'singular_name' => 'Product',
    );
    $args = array(
      'labels'      => $labels,
      'description' => 'Đâylà Custom Post Type KProduct',
      'menu_icon'   => KENSHIN_POST_IMAGES_URL . '/icon-setting16x16.png',
      'public'      => true,
    );
    register_post_type('kproduct', $args); // Phươngthứcđăngký&nạp custom post vàohệthống.
  }
}

Vàđâylàkếtquảsaukhitôi run đoạn code trên.

Giảithích: Phầnthamsố labels chínhláphầnkhaibáotênhiểnthị Custom Post trongvùng admin, bao gồmcácgiátrịkhácmàtôisắptrìnhbày ở phíadưới.

Sử dụng WordPress Custom Post Type căn bản

kproduct-menu-admin

Bạnchú ý phần slug màtôitôđỏtronghìnhnha, bạncóthấylàkiểu post_type=kproduct và ở phíadướicóđầyđủtínhnăngđể post mớimộtbàiviếtgiốngnhưhaikiểu Post & Page.

Tiếptheotôisẽthayđổimộtsốgiátrịnửađểnóphùhợpvớikiểu Custom Post màtôiđangxâydựngvàtôibổ sung code nhưsau.

<?php
class Kenshin_Custom_Post_Product {
  public function __construct() {
    add_action('init', [
      $this,
      'create',
    ]);
  }
  public function create() {
    $labels = [
      'name'          => 'Products',
      'add_new'       => 'Add Product',
      'add_new_item'  => 'Add New Product',
      'search_items'  => 'Search Product',
      'view_item'     => 'View product',
      'edit_item'     => 'Edit product',
      'singular_name' => 'Product',
    ];
    $args = [
      'labels'       => $labels,
      'description'  => 'Đâylà Custom Post Type KProduct',
      'menu_icon'    => KENSHIN_POST_IMAGES_URL . '/icon-setting16x16.png',
      'public'       => true,
      'hierarchical' => true,
    ];
    register_post_type('kproduct', $args); // Phươngthứcđăngký&nạp custom post vàohệthống.
  }
}

Bạnvàohaiđườngdẫnnàyđểkiểmtrakếtquả.

· http://localhost/wordpress/wp-admin/edit.php?post_type=kproduct

· http://localhost/wordpress/wp-admin/post-new.php?post_type=kproduct

Bạntựthayđổiđườngdẫnchophùhợpvớithưmụcchứa source WordPress củabạnnha. Tạmổnrồiđấynhưngmọithứchưadừnglạiđâu, vì ở bài custom post type nângcao tôisẽhướngdẫnbạnlàmnhiềuthứthúvịvàhấpdẫnhơn.

4/ Lờikết

Hy vọngthông qua bài Sửdụng WordPress Custom Post Type cănbản thìbạnđãphầnnàobiếtcáchtạomớicáckiểuđịnhdạng bàiviếttheo ý đồcủariêngbạn, tuynhiênđểlàmđượcđiềunàythìbạncầnphảinắmvữngmộtsốkiếnthứcmàtôiđãtrìnhbày ở cácbàitrước, ápdụngtoànbộkiếnthứcđóđểtạoriêngchomìnhmộtkiểubàiviếtmới. Vàtôicũngxinnóiluônlàkểtừbàinàythìmứcđộkhósẽtăngdần, cácbạnchuẩnbịdầntâmlýđilàvừa.

Series Navigation<<Hướngdẫnxâydựng WordPress Widget VideoTìmhiểu WordPress Meta boxes >>

Nguồn: laptrinhweb.org

Last updated