[WordPress] วิธีการสร้าง Custom Post Type อย่างง่าย

สำหรับ WordPress นั้น เราจะทราบกันอยู่แล้วว่า บทความของเราจะถูกแบ่งออกเป็น Post กับ Page ซึ่งทั้งสองอย่างก็มีคุณสมบัติที่แตกต่างกันอยู่ โดย Post และ Page นี้เราเรียกมันว่า Post Type แต่เราก็ยังสามารถสร้าง Post Type ชนิดใหม่ขึ้นมา นอกเหนือจาก Post และ Page ได้ เราเรียกว่า “Custom Post Type”

Custom Post Type

Custom Post Type นั้นเหมือนเราแยกบทความของเราออกจากหัวข้อ Post หรือ Page มาเป็นหัวข้อของมันเอง อย่างเช่น เราต้องการแยกออกมาเป็นหัวข้อ Book, Product หรือเป็น Customer และ Custom Post Type จะมีประโยชน์มากขึ้นจากการที่เราใช้มันร่วมกับ Custom Field โดยเราอาจจะกำหนดว่า Book ให้ใช้ Custom Field ชนิด A แล้ว Customer ให้ใช้ Custom Field ชนิด B

วิธีสร้าง Custom Post Type

เรามีวิธีสร้าง Custom Post Type ด้วยการใส่ Code ลงไปใน functions.php ซึ่งข้อมูลการใช้งานอยู่ที่หน้า Post Type โดยคำสั่งหลักที่ใช้งานในการสร้างก็คือ register_post_type()

ตัวอย่างการใช้งาน

[php]
add_action(‘init’, ‘product_register’);

function product_register() {
$args = array(
‘label’ => __(‘Products’),
‘singular_label’ => __(‘Product’),
‘public’ => true,
‘show_ui’ => true,
‘capability_type’ => ‘post’,
‘hierarchical’ => false,
‘rewrite’ => true,
‘supports’ => array(‘title’, ‘editor’, ‘thumbnail’)
);

register_post_type( ‘product’ , $args );
}
[/php]

แต่วิธีนี้เราต้องยุ่งยาก วุ่นวายในการเขียน code เพื่อตั้งค่าการใช้งานต่าง ๆ วิธีที่ง่ายกว่าในการสร้าง Custom Post Type ก็คือเราจะใช้ Plugin มาช่วย ซึ่ง Plugin ที่เราใช้ก็คือ Custom Post Type UI

วิธีสร้าง Custom Post Type อย่างง่าย

วิธีการติดตั้ง Custom Post Type UI ก็ไม่ยากเลยครับ

ในส่วนตั้งค่าเพิ่มเติมแต่ละตัวนั้น มีความหมายตามนี้ครับ

  • Public : เลือกระหว่าง True หรือ False โดยการเลือกจะมีผลกับ Exclude From Search, Query Var, Show UI รวมถึงไม่โชว์ในการเลือกเป็น Menu ในหัวข้อ Appearance => Menus
  • Show UI : เลือกระหว่าง True หรือ False หากเลือก False จะทำให้หัวข้อ Custom Post Type ที่สร้างไว้หายไป
  • Has Archive : เลือกระหว่าง True หรือ False ถ้าเลือก True เราจะสามารถโชว์ List ของ Custom Post Type ที่เราสร้างขึ้นมาได้
  • Exclude From Search : เลือก True เพื่อให้ไม่สามารถ Search ได้ในการ Search ด้วยระบบของ WordPress เอง
  • Capability Type : ใส่ระหว่าง post หรือ page เพื่อให้ custom post type นี้มีคุณลักษณะเหมือน post หรือ page
  • Hierarchical : เมื่อเลือก True ตัว custom post type ที่เราสร้างขึ้นจะสามารถเลือกให้บทความใดเป็นบทความย่อยของอีกบทความได้ เหมือน page ที่เราสามารถเลือกหัวข้อไหนเป็น parent ได้
  • Rewrite : เลือก True แล้ว จะเชื่อมต่อกับ Custom Rewrite Slug
  • Custom Rewrite Slug : ถ้าเลือก Rewrite เป็น True ในหัวข้อนี้เราสามารถกำหนดคำที่ใช้ใน url ได้
  • Query Var : ถ้าเราเลือก False จะทำให้เราไม่สามารถ query ข้อมูลของ custom post type ออกมาได้
  • Menu Position : ใส่ตัวเลขเพื่อกำหนดตำแหน่งที่จะวางหัวข้อ custom post type ที่เราสร้างขึ้น โดยปรกติถ้าเราไม่ใส่ตัวเลขอะไรไว้ หัวข้อที่สร้างขึ้นจะอยู่ด้านล่างของ comment โดยค่าตัวเลขต่าง ๆ มีรายละเอียดดังนี้
    • 5 – อยู่ใต้หัวข้อ Posts
    • 10 – อยู่ใต้หัวข้อ Media
    • 15 – อยู่ใต้หัวข้อ Links
    • 20 – อยู่ใต้หัวข้อ Pages
    • 25 – อยู่ใต้หัวข้อ comments
    • 60 – อยู่ใต้เส้นแบ่งอันแรก
    • 65 – อยู่ใต้หัวข้อ Plugins
    • 70 – อยู่ใต้หัวข้อ Users
    • 75 – อยู่ใต้หัวข้อ Tools
    • 80 – อยู่ใต้หัวข้อ Settings
    • 100 – อยู่ใต้เส้นแบ่งอันสอง
  • Show in Menu : ถ้าเลือก False หัวข้อ custom post type ที่เราสร้างขึ้นจะไม่โชว์ที่เมนูด้านซ้ายมือ แต่ถ้าเลือก True เราสามารถกรอกเพื่อให้หัวข้อที่เราสร้างไปเป็นหัวข้อย่อยให้หัวข้อใด โดยเราสามารถหาว่าสิ่งที่ใช้กรอกจาก url ได้
  • Supports : เราสามารถเลือกให้ custom post type ของเราเก็บข้อมูลอะไรบ้าง มีความสามารถอะไรบ้าง
  • Built-in Taxonomies : เลือกว่าให้มี category หรือ tag หรือ taxonomy ให้ใช้งานใน custom post type ที่เราสร้างขึ้นหรือเปล่า

เป็นอันเสร็จสิ้นในการสร้าง custom post type อย่างง่าย ๆ โดยใช้ Plugin ตัว Custom Post Type UI ช่วยในการสร้าง ลองนำไปประยุกต์ใช้งานกับเว็บเราดูนะครับ


Posted

in

by

Comments

9 responses to “[WordPress] วิธีการสร้าง Custom Post Type อย่างง่าย”

  1. […] ส่วน custom post นั้น ก็จะใช้ slug ของตัว custom post นั้นที่เราสร้างขึ้น วิธีสร้าง custom post ก็ดูตามอันนี้เลยครับ [WordPress] วิธีการสร้าง Custom Post Type อย่างง่าย […]

  2. […] ส่วน custom post นั้น ก็จะใช้ slug ของตัว custom post นั้นที่เราสร้างขึ้น วิธีสร้าง custom post ก็ดูตามอันนี้เลยครับ [WordPress] วิธีการสร้าง Custom Post Type อย่างง่าย […]

  3. tudut Avatar

    @rabbitinblack ครับ ผมลองใช้ custom post type UI แล้ว เพียงแต่อยากทราบว่าหากต้องการทำ menu มายังหน้ารวมของ post ที่เรา custom มาเนี่ยะ ต้องทำอย่างไรครับ ยกตัวอย่างเช่น https://9livings.com/property-sale/%E0%B8%97%E0%B8%B2%E0%B8%A7%E0%B8%99%E0%B9%8C%E0%B9%80%E0%B8%AE%E0%B9%89%E0%B8%B2%E0%B8%AA%E0%B9%8C-1-%E0%B8%8A%E0%B8%B1%E0%B9%89%E0%B8%99-%E0%B8%AB%E0%B8%A5%E0%B8%B1%E0%B8%87%E0%B8%AA%E0%B8%A7/ แล้วผมอยากจะทำเมนูให้มาที่หน้า https://9livings.com/property-sale เฉย ๆ อ่าครับ ตอนนี้มันขึ้น ERROR 404 อ่าครับ

    1. rabbitinblack Avatar
      rabbitinblack

      Has Achrive เลือกเป็น True หรือยังครับ

      1. tudut Avatar

        true แล้วครับ

      2. tudut Avatar

        register_post_type(‘property-sale’, array( ‘label’ => ‘sale’,’description’ => ”,’public’ => true,’show_ui’ => true,’show_in_menu’ => true,’capability_type’ => ‘post’,’hierarchical’ => false,’rewrite’ => array(‘slug’ => ”),’query_var’ => true,’has_archive’ => true,’exclude_from_search’ => false,’supports’ => array(‘title’,’editor’,’excerpt’,’trackbacks’,’custom-fields’,’comments’,’revisions’,’thumbnail’,’author’,’page-attributes’,),’taxonomies’ => array(‘category’,’post_tag’,),’labels’ => array (

        ‘name’ => ‘sale’,

        ‘singular_name’ => ‘sale’,

        ‘menu_name’ => ‘sale’,

        ‘add_new’ => ‘Add sale’,

        ‘add_new_item’ => ‘Add New sale’,

        ‘edit’ => ‘Edit’,

        ‘edit_item’ => ‘Edit sale’,

        ‘new_item’ => ‘New sale’,

        ‘view’ => ‘View sale’,

        ‘view_item’ => ‘View sale’,

        ‘search_items’ => ‘Search sale’,

        ‘not_found’ => ‘No sale Found’,

        ‘not_found_in_trash’ => ‘No sale Found in Trash’,

        ‘parent’ => ‘Parent sale’,

        ),) );

        นี่เลยฮ๊าฟฟฟฟ

      3. tudut Avatar

        ได้แบ้ว ๆ ขอบคุณครับ

  4. puripas2it Avatar
    puripas2it

    ขอบคุณมากเลยครับ สำหรับความรู้ดีดี

  5. วิไส Avatar
    วิไส

    ขอบคุณมากเลยครับ สำหรับความรู้ดีดี

Leave a Reply to tudut Cancel reply

Your email address will not be published. Required fields are marked *