โดย thatsawan » 25/10/2016 12:02 pm
Custom Fields
- การสร้างฟิลด์ใน Post type ต่างๆ เพื่อให้สามารถเก็บค่าต่างๆ เป็นการเพิ่มความสามารถให้กับ WordPress
ตัวอย่างการใช้งาน
ส่วนเเรกเป็นส่วนที่เพิ่ม Post type (ศึกษาได้ในบทก่อนหน้านี้)
โค้ด: เลือกทั้งหมด
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 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' )
);
$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 );
}
ส่วนที่ 2 เป็นส่วนที่จะเพิ่ม Custom Fields
โค้ด: เลือกทั้งหมด
function custom_meta_box_markup()
{
echo '<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" />';
}
function add_custom_meta_box()
{
add_meta_box("demo-meta-box", "Custom Meta Box", "custom_meta_box_markup", "book", "normal", "high", null);
}
add_action("add_meta_boxes", "add_custom_meta_box");
ผลที่ได้
ตัวอย่างการใช้งาน wp_editor()
โค้ด: เลือกทั้งหมด
function custom_meta_box_markup()
{
echo '<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" />';
$content = get_post_meta($post->ID,'custom_fields',true);
$editor_id = 'mycustomeditor';
$setting = array(
'textarea_rows' => 8,
'media_buttons' => true, // show button add media
);
wp_editor( $content, $editor_id,$setting );
}
function add_custom_meta_box()
{
add_meta_box("demo-meta-box", "Custom Meta Box", "custom_meta_box_markup", "book", "normal", "high", null);
}
add_action("add_meta_boxes", "add_custom_meta_box");
ผลที่ได้
ศึกษาเพิ่มเติมที่นี่
https://codex.wordpress.org
[b]Custom Fields [/b]
[list] การสร้างฟิลด์ใน Post type ต่างๆ เพื่อให้สามารถเก็บค่าต่างๆ เป็นการเพิ่มความสามารถให้กับ WordPress[/list]
[b]ตัวอย่างการใช้งาน [/b]
ส่วนเเรกเป็นส่วนที่เพิ่ม Post type (ศึกษาได้ในบทก่อนหน้านี้)
[code=php]
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 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' )
);
$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 );
}[/code]
ส่วนที่ 2 เป็นส่วนที่จะเพิ่ม Custom Fields
[code=php]function custom_meta_box_markup()
{
echo '<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" />';
}
function add_custom_meta_box()
{
add_meta_box("demo-meta-box", "Custom Meta Box", "custom_meta_box_markup", "book", "normal", "high", null);
}
add_action("add_meta_boxes", "add_custom_meta_box"); [/code]
[b]ผลที่ได้ [/b]
[img]http://snap.mindphp.com/index.php?view=2016Oct24_580e31b6d2edc[/img]
[b]ตัวอย่างการใช้งาน wp_editor() [/b]
[code=php]function custom_meta_box_markup()
{
echo '<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" />';
$content = get_post_meta($post->ID,'custom_fields',true);
$editor_id = 'mycustomeditor';
$setting = array(
'textarea_rows' => 8,
'media_buttons' => true, // show button add media
);
wp_editor( $content, $editor_id,$setting );
}
function add_custom_meta_box()
{
add_meta_box("demo-meta-box", "Custom Meta Box", "custom_meta_box_markup", "book", "normal", "high", null);
}
add_action("add_meta_boxes", "add_custom_meta_box");
[/code]
[b]ผลที่ได้ [/b]
[img]http://snap.mindphp.com/index.php?view=2016Oct24_580e3275e5519[/img]
[b]ศึกษาเพิ่มเติมที่นี่[/b]
https://codex.wordpress.org