สอนสร้าง Plugin WordPress (13) : ตัวอย่างการประกาศคุณสมบัติของ Custom Post Types

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: สอนสร้าง Plugin WordPress (13) : ตัวอย่างการประกาศคุณสมบัติของ Custom Post Types

Re: สอนสร้าง Plugin WordPress (13) : ตัวอย่างการประกาศคุณสมบัติของ Custom Post Types

โดย บุคคลทั่วไป » 04/04/2023 8:54 am

ขอบคุณมากๆ เลย

Re: สอนสร้าง Plugin WordPress (13) : ตัวอย่างการประกาศคุณสมบัติของ Custom Post Types

โดย mindphp » 25/10/2016 9:48 pm

รวมกระทู้ บทความสอนสร้าง Plugin WordPress
https://www.mindphp.com/forums/viewtopic ... 25&t=36079

สอนสร้าง Plugin WordPress (13) : ตัวอย่างการประกาศคุณสมบัติของ Custom Post Types

โดย thatsawan » 25/10/2016 12:02 pm

การประกาศคุณสมบัติของ Custom Post Types หมายความว่า เราสามารถกำหนดรายละเอียดของ Post type เราได้ ยกตัวอย่างเช่น Post type ที่เราสร้างสามารถ Add ใน Menu ของ WordPress ได้หรือไม หรือ การกำหนดชื่อ Button จุดต่างๆ ให้เป็นของเราเอง

โดยมีคุณสมบัติหรือ Parameter ดังตัวอย่างนี้

ตัวอย่างการระบุชื่อชื่อของ Button ต่างๆ

โค้ด: เลือกทั้งหมด

 $labels = array(
    'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
    'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
    'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
    'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
    'add_new'            => _x( 'Add News Demo', 'book', 'your-plugin-textdomain' ),
    'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
    'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
    'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
    'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
    'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
    'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
    'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
    'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
    'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
  ); 
รูปภาพ



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

โค้ด: เลือกทั้งหมด

add_action( 'init', 'codex_book_init' );

/**
 * Register a book post type.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_post_type
 */
function codex_book_init() {
  $labels = array(
    'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
    'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
    'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
    'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
    'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
    'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
    'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
    'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
    'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
    'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
    'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
    'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
    'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
    'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
  );

  $args = array(
    'labels'             => $labels,
    'description'        => __( 'Description.', 'your-plugin-textdomain' ),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'show_in_nav_menus' => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'book' ),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => null,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  );

  register_post_type( 'book', $args );
} 

คำอธิบายเพิ่มเติม
จากตัวอย่างเราได้กำหนด

โค้ด: เลือกทั้งหมด

   'show_in_nav_menus' => true, 
มีความหมายว่าต้องการให้ Menu สามารถถึงข้อมูลจาก Post Type เราไปใช้งานได้ นั้นเอง
รูปภาพ


ศึกษาเพิ่มเติมได้ที่นี่
https://codex.wordpress.org/Function_Re ... _post_type

ข้างบน