Skip to content

Hi You, I am Tade

  • About
  • Donate
  • Privacy policy
    • About
    • Donate
    • Privacy policy

Cách tạo meta box trong custom post wordpress

  • Hoàng Thức
  • May 6, 2016

Post meta hay còn gọi là custom field được wordpress cấu hình sẳn nó là những giá trị text field cho phép nhập các giá trị text. Nhưng khi ta muốn tùy biến các text field này thành các dạng dữ liệu như ngày tháng năm,  số hoặc chuyển từ kiểu text field sang kiểu select ta sẽ sử dụng một phương thức gọi là meta box. Các biến giá trị trong meta box sẽ được gọi là post meta. Và không lòng vòng nữa đây là phần code:

1.Khỏi tạo Meta Box 


function wpdocs_register_meta_boxes() {
add_meta_box( 'meta-box-id', __( 'INFO', 'WA' ), 'wpdocs_my_display_callback', 'post' );
}
add_action( 'add_meta_boxes', 'wpdocs_register_meta_boxes' );

ở đây ta chú ý post type là “post” nhé các bạn có thể tùy chỉnh theo từng loại post type ví dụ “page” cho trang.

2.Nội dung hàm hiển thị Meta box


function wpdocs_my_display_callback( ) {
global $post;
$meta_post_title = get_post_meta($post->ID, 'meta_post_title', true);
$post_desc = get_post_meta($post->ID, 'post_desc', true);
?>
<table class="form-table">
<tr>
<th>
<label>Title</label>
</th>
<td>
<textarea name="meta_post_title" class="large-text"><?php echo $meta_post_title; ?></textarea>
</td>
</tr>
<tr>
<th>
<label>Description</label>
</th>
<td>
<textarea name="post_desc" class="large-text"><?php echo $post_desc; ?></textarea>
</td>
</tr>
</table>
<?php
}

3. Hàm lưu lại giá trị


add_action( 'save_post', 'wpdocs_save_meta_box' );
function wpdocs_save_meta_box() {
global $post;
$quote_post_meta['meta_post_title'] = $_POST['meta_post_title'];
$quote_post_meta['post_desc'] = $_POST['post_desc'];

foreach( $quote_post_meta as $key => $value ) { // cycle through the $quote_post_meta array
$value = implode(',', (array)$value); // if $value is an array, make it a CSV (unlikely)
if( get_post_meta( $post->ID, $key, FALSE ) ) { // if the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else {
add_post_meta( $post->ID, $key, $value );
}
if( !$value ) {
delete_post_meta( $post->ID, $key );
}
}
}

Chúc các bạn thành công nhé !

    Tags:
  • wordpress nâng cao
    Share

Post navigation

←Previous:
Next:→

44 thoughts on “Cách tạo meta box trong custom post wordpress”

  1. Pingback: Wordpress nâng cao 1: Custom post type - HoangThuc
  2. eebest8 back says:
    8 July, 2016 at 8:10 am

    “Thank you, I have recently been searching for info about this topic for a long time and yours is the best I’ve discovered so far. However, what concerning the conclusion? Are you sure concerning the source?”

  3. Pingback: Cotton Tote Bag Manufacturer
  4. Pingback: link
  5. Pingback: Diyala Bauc14
  6. Pingback: big 666 สล็อต
  7. Pingback: บุหรี่นอก
  8. Pingback: ผู้ป่วยติดเตียง
  9. Pingback: sell nuclear weapons
  10. Pingback: หวยยี่กี หวยปิงปอง แทงง่าย เล่นได้ทั้งวัน
  11. Pingback: Buy BCM® MK2 Standard 9" 300 BLACKOUT Upper Receiver Group w/ MCMR-8 Handguard  Online
  12. Pingback: Science
  13. Pingback: Scatter สัญลักษณ์สล็อต คืออะไร
  14. Pingback: hit789
  15. Pingback: จอ led ขนาดใหญ่
  16. Pingback: sci/sci news/news sci/ science diyala
  17. Pingback: ตู้แช่แข็ง
  18. Pingback: ทางเข้าpg168
  19. Pingback: cat888
  20. Pingback: hit789
  21. Pingback: highbay
  22. Pingback: bus ticket online
  23. Pingback: 789bet
  24. Pingback: ซักผ้าโรงแรม
  25. Pingback: ufa789
  26. Pingback: fruit party slot uk
  27. Pingback: บุหรี่นอก
  28. Pingback: ระบบสมาชิก
  29. Pingback: מה עושים עם תלונת שווא במשטרה
  30. Pingback: read article
  31. Pingback: micialisno.net
  32. Pingback: aromasin dosage for bodybuilding
  33. Pingback: what is vidalista
  34. Pingback: viagra 100mg
  35. Pingback: เว็บตรงฝากถอนง่าย
  36. Pingback: super filagra 160 mg
  37. Pingback: heng678
  38. Pingback: what is levitra used for
  39. Pingback: health20253.wordpress.com
  40. Pingback: forceforu.wordpress.com
  41. Pingback: priliforyou.wordpress.com
  42. Pingback: furosemide 80 mg oral tablet
  43. Pingback: neuronforyou.wordpress.com
  44. Pingback: cathopic.com/@iverscab

Comments are closed.

©2025 Hi You, I am Tade / Designed & Built by WPFound