การสร้าง Custom Post และ Taxonomy ด้วย PHP

การสร้าง Custom Post และ Taxonomy ด้วย PHP

Custom Post คืออะไร

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

Taxonomy คืออะไร และ ต่างจาก Category ที่ wordpress ให้มาอย่างไร

Taxonomy ใน WordPress คือ ระบบการจัดกลุ่มเนื้อหาทุกประเภท (ทั้ง Post, Page, Custom Post Type) ให้เป็นหมวดหมู่หรือแท็กตามความสัมพันธ์ของเนื้อหานั้นๆ คำว่า “Taxonomy” เป็นคำศัพท์กว้างๆ ที่หมายถึงหลักการหรือวิธีการจัดหมวดหมู่

Categories ที่ WordPress ให้มานั้นเป็นเพียงหนึ่งใน Taxonomy เริ่มต้น (Default Taxonomy) ที่มีมาพร้อมกับ WordPress

ความแตกต่างระหว่าง Taxonomy กับ Category ใน WordPress:

  • Taxonomy เป็นคำศัพท์ที่กว้างกว่า: Taxonomy หมายถึงระบบการจัดกลุ่มทั้งหมด ในขณะที่ Category เป็นเพียงรูปแบบหนึ่งของ Taxonomy
  • WordPress มี Default Taxonomies อื่นๆ: นอกเหนือจาก Categories แล้ว WordPress ยังมี Tags เป็น Default Taxonomy อีกด้วย และคุณยังสามารถสร้าง Custom Taxonomies เพิ่มเติมได้ตามความต้องการ
  • โครงสร้าง:
    • Categories: มีโครงสร้างแบบ ลำดับชั้น (Hierarchical) นั่นหมายความว่าคุณสามารถสร้างหมวดหมู่ย่อย (Child Categories) ภายใต้หมวดหมู่หลัก (Parent Categories) ได้
    • Tags: ไม่มีโครงสร้างแบบลำดับชั้น เป็นแบบ แบนราบ (Non-hierarchical) คุณไม่สามารถสร้างแท็กย่อยภายใต้แท็กหลักได้
    • Custom Taxonomies: สามารถกำหนดได้ว่าจะเป็นแบบมีลำดับชั้นหรือไม่ตอนสร้าง
  • วัตถุประสงค์:
    • Categories: ใช้สำหรับการจัดกลุ่มเนื้อหาใน ภาพรวมกว้างๆ เหมือนสารบัญของหนังสือ ช่วยให้ผู้เข้าชมเข้าใจหัวข้อหลักของเว็บไซต์
    • Tags: ใช้สำหรับการระบุ รายละเอียดเฉพาะเจาะจง หรือ คำหลัก ที่เกี่ยวข้องกับเนื้อหา เหมือนดัชนีคำศัพท์ของหนังสือ
    • Custom Taxonomies: สร้างขึ้นเพื่อจัดกลุ่มเนื้อหาตามความต้องการเฉพาะของเว็บไซต์ เช่น “ประเภทสินค้า”, “แบรนด์”, “ผู้แต่ง” เป็นต้น

สรุปง่ายๆ: Taxonomy คือวิธีการจัดกลุ่มเนื้อหา Categories และ Tags เป็นเพียงสองรูปแบบเริ่มต้นของ Taxonomy ที่ WordPress มีให้ และคุณยังสามารถสร้าง Taxonomy อื่นๆ เพิ่มเติมได้เอง

การสร้าง Custom Post Type และ Taxonomy จะช่วยให้คุณจัดการเนื้อหาบนเว็บไซต์ WordPress ได้เป็นระเบียบ

สร้าง Custom Post และ Taxonomy ใช้เอง เพราะความเป็นระเบียบ

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

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

มีอยู่หลายวิธีในการสร้าง Custom Post Type ครับ แต่สองวิธีหลักๆ ที่นิยมใช้กันคือ:

1. การใช้ Plugin:

วิธีนี้ง่ายและรวดเร็ว เหมาะสำหรับผู้ที่ไม่ต้องการเขียนโค้ด Plugin ฟรีและเสียเงินมากมายที่ช่วยให้คุณสร้าง Custom Post Type ได้ง่ายๆ ผ่านหน้า Dashboard ของ WordPress ตัวอย่าง Plugin ที่ได้รับความนิยม เช่น:

  • Custom Post Type UI: เป็น Plugin ที่ใช้งานง่าย มีผู้ใช้งานเยอะ และมีฟีเจอร์ครบครันสำหรับการสร้างและจัดการ Custom Post Type และ Taxonomy
  • Pods – Custom Content Types and Fields: เป็น Plugin ที่มีความสามารถขั้นสูงกว่า สามารถสร้าง Custom Fields และความสัมพันธ์ระหว่าง Post Type ได้อย่างซับซ้อน

ขั้นตอนการใช้งาน Plugin (ตัวอย่าง Custom Post Type UI):

  1. ติดตั้งและเปิดใช้งาน Plugin: ไปที่ “Plugins” -> “Add New” แล้วค้นหา “Custom Post Type UI” ติดตั้งและเปิดใช้งาน Plugin
  2. สร้าง Custom Post Type: ไปที่เมนู “CPT UI” -> “Add/Edit Post Types”
  3. กรอกรายละเอียด:
    • Post Type Slug: ชื่อเฉพาะของ Post Type ที่จะใช้ในโค้ด (ตัวพิมพ์เล็ก, ไม่เว้นวรรค, ใช้ขีดล่างแทนได้ เช่น products)
    • Plural Label: ชื่อที่แสดงในเมนู WordPress ในรูปแบบพหูพจน์ (เช่น สินค้า)
    • Singular Label: ชื่อที่แสดงในเมนู WordPress ในรูปแบบเอกพจน์ (เช่น สินค้า)
    • Options: กำหนดค่าต่างๆ เพิ่มเติม เช่น รองรับ Editor, Featured Image, Taxonomies ที่จะใช้ร่วมกัน, ตำแหน่งในเมนู, ไอคอน และอื่นๆ
  4. กด “Add Post Type”: Custom Post Type ใหม่ของคุณก็จะถูกสร้างขึ้น

2. การเขียนโค้ดใน functions.php หรือ Plugin:

วิธีนี้จะมีความยืดหยุ่นสูงกว่า เหมาะสำหรับผู้ที่ต้องการควบคุมการทำงานของ Custom Post Type อย่างละเอียด หรือต้องการรวม Custom Post Type เข้ากับ Plugin ที่พัฒนาเอง

ตัวอย่างโค้ดใน functions.php หรือ Plugin:

<?php
function create_product_post_type() {
    $labels = array(
        'name'               => _x( 'สินค้า', 'Post Type General Name', 'your_theme_textdomain' ),
        'singular_name'      => _x( 'สินค้า', 'Post Type Singular Name', 'your_theme_textdomain' ),
        'menu_name'          => __( 'สินค้า', 'your_theme_textdomain' ),
        'name_admin_bar'     => __( 'สินค้า', 'your_theme_textdomain' ),
        'parent_item_colon'  => __( 'สินค้าหลัก:', 'your_theme_textdomain' ),
        'all_items'          => __( 'สินค้าทั้งหมด', 'your_theme_textdomain' ),
        'add_new_item'       => __( 'เพิ่มสินค้าใหม่', 'your_theme_textdomain' ),
        'add_new'            => __( 'เพิ่มใหม่', 'your_theme_textdomain' ),
        'new_item'           => __( 'สินค้าใหม่', 'your_theme_textdomain' ),
        'edit_item'          => __( 'แก้ไขสินค้า', 'your_theme_textdomain' ),
        'update_item'        => __( 'อัปเดตสินค้า', 'your_theme_textdomain' ),
        'view_item'          => __( 'ดูสินค้า', 'your_theme_textdomain' ),
        'search_items'       => __( 'ค้นหาสินค้า', 'your_theme_textdomain' ),
        'not_found'          => __( 'ไม่พบสินค้า', 'your_theme_textdomain' ),
        'not_found_in_trash' => __( 'ไม่พบสินค้าในถังขยะ', 'your_theme_textdomain' ),
    );
    $args = array(
        'label'               => __( 'สินค้า', 'your_theme_textdomain' ),
        'description'         => __( 'จัดการสินค้าต่างๆ', 'your_theme_textdomain' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ), // คุณสมบัติที่รองรับ
        'taxonomies'          => array( 'product_category', 'product_tag' ), // Taxonomies ที่จะใช้ร่วมกัน
        'hierarchical'        => false, // กำหนดให้เป็นแบบลำดับชั้นหรือไม่ (เหมือน Page หรือ Post)
        'public'              => true, // แสดงในส่วน Front-end หรือไม่
        'show_ui'             => true, // แสดงใน Admin Dashboard หรือไม่
        'show_in_menu'        => true, // แสดงในเมนูหลักหรือไม่
        'menu_position'       => 5, // ตำแหน่งในเมนู
        'menu_icon'           => 'dashicons-cart', // ไอคอนในเมนู
        'show_in_admin_bar'   => true,
        'show_in_nav_menus'   => true,
        'can_export'          => true,
        'has_archive'         => true, // สร้างหน้า Archive หรือไม่
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'rewrite'             => array( 'slug' => 'products' ), // กำหนด Slug สำหรับ URL
    );
    register_post_type( 'product', $args );
}
add_action( 'init', 'create_product_post_type', 0 );
?>

คำอธิบายโค้ด:

  • create_product_post_type(): ชื่อฟังก์ชันที่คุณกำหนด
  • $labels: Array ที่เก็บข้อความต่างๆ ที่จะแสดงในส่วน Admin
  • $args: Array ที่กำหนดคุณสมบัติและตัวเลือกต่างๆ ของ Custom Post Type
    • label: ชื่อ Custom Post Type ที่จะแสดง
    • description: คำอธิบายของ Custom Post Type
    • labels: เรียกใช้งาน Array $labels
    • supports: กำหนดคุณสมบัติที่ Custom Post Type นี้รองรับ เช่น title (ชื่อเรื่อง), editor (ตัวแก้ไขเนื้อหา), thumbnail (รูปภาพ Featured), excerpt (เนื้อหาสรุป), custom-fields (Custom Fields)
    • taxonomies: กำหนด Taxonomies ที่จะใช้ร่วมกับ Custom Post Type นี้
    • hierarchical: กำหนดให้ Custom Post Type มีลำดับชั้นเหมือน Page หรือไม่ (true/false)
    • public: กำหนดให้สามารถเข้าถึงได้จาก Front-end หรือไม่ (true/false)
    • show_ui: กำหนดให้แสดงใน Admin Dashboard หรือไม่ (true/false)
    • show_in_menu: กำหนดให้แสดงเป็นเมนูหลักหรือไม่ (true/false)
    • menu_position: กำหนดตำแหน่งของเมนูใน Admin Dashboard (ตัวเลขยิ่งน้อยจะอยู่ด้านบน)
    • menu_icon: กำหนดไอคอนที่จะแสดงในเมนู (ใช้ Dashicons)
    • has_archive: กำหนดให้สร้างหน้า Archive สำหรับ Custom Post Type นี้หรือไม่ (true/false)
    • rewrite: กำหนด Slug สำหรับ URL ของ Custom Post Type
  • register_post_type( 'product', $args ): ฟังก์ชันหลักที่ใช้ในการสร้าง Custom Post Type โดย 'product' คือ Post Type Slug ที่คุณกำหนด
  • add_action( 'init', 'create_product_post_type', 0 ): Hook ที่ใช้ในการเรียกฟังก์ชัน create_product_post_type() เมื่อ WordPress เริ่มทำงาน

Custom Taxonomy คือการจัดกลุ่มเนื้อหาใน Custom Post Type หรือ Post รูปแบบเดิมๆ ให้เป็นหมวดหมู่

วิธีการสร้าง Custom Taxonomy

Custom Taxonomy คือการจัดกลุ่มเนื้อหาใน Custom Post Type หรือ Post รูปแบบเดิมๆ ให้เป็นหมวดหมู่หรือแท็กตามที่คุณต้องการ เช่น หากคุณมี Custom Post Type ชื่อ “สินค้า” คุณอาจต้องการสร้าง Taxonomy ชื่อ “ประเภทสินค้า” (เหมือน Categories) และ “แท็กสินค้า” (เหมือน Tags)

เช่นเดียวกับการสร้าง Custom Post Type คุณสามารถสร้าง Custom Taxonomy ได้ 2 วิธีหลักๆ คือ:

1. การใช้ Plugin:

Plugin อย่าง Custom Post Type UI และ Pods ก็สามารถช่วยให้คุณสร้าง Custom Taxonomy ได้ง่ายๆ ผ่านหน้า Dashboard

ขั้นตอนการใช้งาน Plugin (ตัวอย่าง Custom Post Type UI):

  1. ไปที่เมนู “CPT UI” -> “Add/Edit Taxonomies”
  2. กรอกรายละเอียด:
    • Taxonomy Slug: ชื่อเฉพาะของ Taxonomy ที่จะใช้ในโค้ด (ตัวพิมพ์เล็ก, ไม่เว้นวรรค, ใช้ขีดล่างแทนได้ เช่น product_category)
    • Plural Label: ชื่อที่แสดงในเมนู WordPress ในรูปแบบพหูพจน์ (เช่น ประเภทสินค้า)
    • Singular Label: ชื่อที่แสดงในเมนู WordPress ในรูปแบบเอกพจน์ (เช่น ประเภทสินค้า)
    • Attach to Post Types: เลือก Custom Post Type หรือ Post Types ที่คุณต้องการให้ Taxonomy นี้ใช้งานร่วมด้วย (ในตัวอย่างนี้คือ สินค้า)
    • Options: กำหนดค่าต่างๆ เพิ่มเติม เช่น กำหนดให้เป็นแบบลำดับชั้น (เหมือน Categories) หรือไม่ (เหมือน Tags), แสดงใน Admin Columns, และอื่นๆ
  3. กด “Add Taxonomy”: Custom Taxonomy ใหม่ของคุณก็จะถูกสร้างขึ้น

2. การเขียนโค้ดใน functions.php หรือ Plugin:

ตัวอย่างโค้ดใน functions.php หรือ Plugin:

<?php
function create_product_taxonomy() {
    $labels = array(
        'name'                       => _x( 'ประเภทสินค้า', 'Taxonomy General Name', 'your_theme_textdomain' ),
        'singular_name'              => _x( 'ประเภทสินค้า', 'Taxonomy Singular Name', 'your_theme_textdomain' ),
        'menu_name'                  => __( 'ประเภทสินค้า', 'your_theme_textdomain' ),
        'all_items'                  => __( 'ประเภทสินค้าทั้งหมด', 'your_theme_textdomain' ),
        'parent_item'                => __( 'ประเภทสินค้าหลัก', 'your_theme_textdomain' ),
        'parent_item_colon'          => __( 'ประเภทสินค้าหลัก:', 'your_theme_textdomain' ),
        'new_item_name'              => __( 'เพิ่มประเภทสินค้าใหม่', 'your_theme_textdomain' ),
        'add_new_item'               => __( 'เพิ่มประเภทสินค้าใหม่', 'your_theme_textdomain' ),
        'edit_item'                  => __( 'แก้ไขประเภทสินค้า', 'your_theme_textdomain' ),
        'update_item'                => __( 'อัปเดตประเภทสินค้า', 'your_theme_textdomain' ),
        'view_item'                  => __( 'ดูประเภทสินค้า', 'your_theme_textdomain' ),
        'separate_items_with_commas' => __( 'คั่นประเภทสินค้าด้วยเครื่องหมายคอมม่า', 'your_theme_textdomain' ),
        'add_or_remove_items'        => __( 'เพิ่มหรือลบประเภทสินค้า', 'your_theme_textdomain' ),
        'choose_from_most_used'      => __( 'เลือกจากประเภทสินค้าที่ใช้บ่อย', 'your_theme_textdomain' ),
        'popular_items'              => __( 'ประเภทสินค้ายอดนิยม', 'your_theme_textdomain' ),
        'search_items'               => __( 'ค้นหาประเภทสินค้า', 'your_theme_textdomain' ),
        'not_found'                  => __( 'ไม่พบประเภทสินค้า', 'your_theme_textdomain' ),
        'no_terms'                   => __( 'ไม่มีประเภทสินค้า', 'your_theme_textdomain' ),
        'items_list'                 => __( 'รายการประเภทสินค้า', 'your_theme_textdomain' ),
        'items_list_navigation'      => __( 'การนำทางรายการประเภทสินค้า', 'your_theme_textdomain' ),
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true, // กำหนดให้เป็นแบบลำดับชั้นหรือไม่ (เหมือน Categories)
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'rewrite'                    => array( 'slug' => 'product-category' ), // กำหนด Slug สำหรับ URL
    );
    register_taxonomy( 'product_category', 'product', $args );

    // สร้าง Taxonomy สำหรับ Tag (ไม่เป็นลำดับชั้น)
    $labels_tag = array(
        'name'                       => _x( 'แท็กสินค้า', 'Taxonomy General Name', 'your_theme_textdomain' ),
        'singular_name'              => _x( 'แท็กสินค้า', 'Taxonomy Singular Name', 'your_theme_textdomain' ),
        'menu_name'                  => __( 'แท็กสินค้า', 'your_theme_textdomain' ),
        'all_items'                  => __( 'แท็กสินค้าทั้งหมด', 'your_theme_textdomain' ),
        'new_item_name'              => __( 'เพิ่มแท็กสินค้าใหม่', 'your_theme_textdomain' ),
        'add_new_item'               => __( 'เพิ่มแท็กสินค้าใหม่', 'your_theme_textdomain' ),
        'edit_item'                  => __( 'แก้ไขแท็กสินค้า', 'your_theme_textdomain' ),
        'update_item'                => __( 'อัปเดตแท็กสินค้า', 'your_theme_textdomain' ),
        'view_item'                  => __( 'ดูแท็กสินค้า', 'your_theme_textdomain' ),
        'separate_items_with_commas' => __( 'คั่นแท็กสินค้าด้วยเครื่องหมายคอมม่า', 'your_theme_textdomain' ),
        'add_or_remove_items'        => __( 'เพิ่มหรือลบแท็กสินค้า', 'your_theme_textdomain' ),
        'choose_from_most_used'      => __( 'เลือกจากแท็กสินค้าที่ใช้บ่อย', 'your_theme_textdomain' ),
        'popular_items'              => __( 'แท็กสินค้ายอดนิยม', 'your_theme_textdomain' ),
        'search_items'               => __( 'ค้นหาแท็กสินค้า', 'your_theme_textdomain' ),
        'not_found'                  => __( 'ไม่พบแท็กสินค้า', 'your_theme_textdomain' ),
        'no_terms'                   => __( 'ไม่มีแท็กสินค้า', 'your_theme_textdomain' ),
        'items_list'                 => __( 'รายการแท็กสินค้า', 'your_theme_textdomain' ),
        'items_list_navigation'      => __( 'การนำทางรายการแท็กสินค้า', 'your_theme_textdomain' ),
    );
    $args_tag = array(
        'labels'                     => $labels_tag,
        'hierarchical'               => false, // ไม่เป็นแบบลำดับชั้น (เหมือน Tags)
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'rewrite'                    => array( 'slug' => 'product-tag' ), // กำหนด Slug สำหรับ URL
    );
    register_taxonomy( 'product_tag', 'product', $args_tag );
}
add_action( 'init', 'create_product_taxonomy', 0 );
?>

คำอธิบายโค้ด:

  • create_product_taxonomy(): ชื่อฟังก์ชันที่คุณกำหนด
  • $labels และ $labels_tag: Array ที่เก็บข้อความต่างๆ ที่จะแสดงในส่วน Admin สำหรับ Taxonomy แต่ละแบบ
  • $args และ $args_tag: Array ที่กำหนดคุณสมบัติและตัวเลือกต่างๆ ของ Custom Taxonomy
    • labels: เรียกใช้งาน Array $labels หรือ $labels_tag
    • hierarchical: กำหนดให้ Taxonomy มีลำดับชั้นเหมือน Categories (true) หรือไม่เหมือน Tags (false)
    • public: กำหนดให้สามารถเข้าถึงได้จาก Front-end หรือไม่ (true/false)
    • show_ui: กำหนดให้แสดงใน Admin Dashboard หรือไม่ (true/false)
    • show_admin_column: กำหนดให้แสดงเป็น Column ในหน้า Edit ของ Post Type ที่ใช้งานร่วมด้วยหรือไม่ (true/false)
    • show_in_nav_menus: กำหนดให้แสดงในเมนู Navigation หรือไม่ (true/false)
    • show_tagcloud: กำหนดให้แสดงใน Widget Tag Cloud ได้หรือไม่ (true/false) (สำหรับ Taxonomy ที่ไม่เป็นลำดับชั้น)
    • rewrite: กำหนด Slug สำหรับ URL ของ Taxonomy
  • register_taxonomy( 'product_category', 'product', $args ): ฟังก์ชันหลักที่ใช้ในการสร้าง Custom Taxonomy โดย 'product_category' คือ Taxonomy Slug และ 'product' คือ Post Type ที่จะใช้งานร่วมด้วย
  • add_action( 'init', 'create_product_taxonomy', 0 ): Hook ที่ใช้ในการเรียกฟังก์ชัน create_product_taxonomy() เมื่อ WordPress เริ่มทำงาน

สิ่งที่ควรทราบ:

  • your_theme_textdomain: ในโค้ดตัวอย่าง จะมี 'your_theme_textdomain' ให้คุณแทนที่ด้วย Text Domain ของ Theme หรือ Plugin ของคุณ เพื่อให้สามารถแปลข้อความต่างๆ ได้
  • Flush Permalinks: หลังจากที่คุณสร้าง Custom Post Type หรือ Taxonomy ใหม่ หรือมีการเปลี่ยนแปลง URL (Slug), คุณจำเป็นต้องเข้าไปที่ Settings -> Permalinks

เป็นไงกันบ้าง นี่เป็นแค่เพียงพื้นฐาน เพื่อให้เห็นภาพรวมเท่านั้น แต่สามารถนำไปต่อยอดในการพัฒนาได้

Spread the love