• Về tôi
  • Chính sách riêng tư
  • Liên hệ

HoangThuc.com

Website Developer

  • Trang chủ
  • WordPress
    • Plugin wordpress
  • Lập trình
    • Php
  • Thủ thuật máy tính
You are here: Home / Wordpress / Cách tạo meta box trong custom post wordpress

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

6 May, 2016 by Hoàng Thức

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é !

Bài viết khác

  • Sử dụng media của wordpress để tải hình ảnhSử dụng media của wordpress để tải hình ảnh
  • Plugin lấy tin tự động version 2.1Plugin lấy tin tự động version 2.1
  • Tạo template trong plugin wordpressTạo template trong plugin wordpress
  • Xóa slug trên custom post và custom taxonomy: Phần 2Xóa slug trên custom post và custom taxonomy: Phần 2
  • Xóa slug trên custom post và custom taxonomy: Phần 1Xóa slug trên custom post và custom taxonomy: Phần 1
  • Hiển thị thời gian kể từ ngày đăng trong wordpressHiển thị thời gian kể từ ngày đăng trong wordpress
Share

Bình luận

Bình luận

Filed Under: Wordpress, Wordpress nâng cao Tagged With: wordpress nâng cao

Comments

  1. 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?”

Trackbacks

  1. Wordpress nâng cao 1: Custom post type - HoangThuc says:
    9 May, 2016 at 12:42 pm

    […] đây mình có làm phần tích hợp meta box nếu các bạn thấy không cần thiết thì bỏ ra […]

Bài viết mới

> 2021/10/11 19:10:10 [INFO] acme: Registering account for admin@X.X.X.X>

  • Dữ liệu JSON trong MySQL
  • Chuyển thư mục public sang public_html trong Laravel khi deploy
  • Fix “Requested icu-uc 50.1 but version of icu-uc is” trên DirectAdmin

FOLLOW ME

  • Facebook
  • Google+
  • Twitter

Danh mục

  • Blog
  • Lập trình
  • Laravel
  • MYSQL
  • Php
  • Thủ thuật máy tính
  • Vps
    • DirectAdmin
  • Wordpress
    • Plugin wordpress
    • Wordpress căn bản
    • Wordpress nâng cao
      • Custom post wordpress

Archives

  • October 2021
  • January 2021
  • December 2020
  • November 2020
  • September 2020
  • August 2020
  • September 2019
  • August 2019
  • July 2019
  • June 2018
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • March 2017
  • November 2016
  • September 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • January 2016
  • December 2015
  • May 2015
  • April 2015
Copyright © 2022 · HoangThuc.com · Genesis Framework