如果已创建自定义帖子类型,则可以像这样添加元框:
add_action( \'add_meta_boxes\', \'add_dates\' );
function add_dates() {
add_meta_box(\'date_select\', \'Dates\', \'date_select\', \'customposttype\', \'side\', \'default\');
}
function date_select() {
global $post;
echo \'<input type="hidden" name="date_select_meta_noncename" id="date_select_meta_noncename" value="\' .
wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';
$gender = get_post_meta($post->ID, \'_date\', true);
// Insert checkboxes HTML
}
然后需要保存自定义元数据。
function save_dates($post_id, $post) {
if ( !wp_verify_nonce( $_POST[\'date_select_meta_noncename\'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !current_user_can( \'edit_post\', $post->ID ))
return $post->ID;
$date_meta[\'_date\'] = $_POST[\'_date\'];
foreach ($date_meta as $key => $value) {
if( $post->post_type == \'revision\' ) return;
$value = implode(\',\', (array)$value);
if(get_post_meta($post->ID, $key, FALSE)) {
update_post_meta($post->ID, $key, $value);
} else {
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key);
}
}
add_action(\'save_post\', \'save_dates\', 1, 2);
当然,您需要为自定义帖子类型设置它。
(警告:未测试)