有一个有效的例子。我用它来创建bbpress自定义元数据库
$cpt_public_slug = \'topic\';
add_action( \'add_meta_boxes_\' . $cpt_public_slug, \'adding_custom_meta_boxes\' );
add_action( \'save_post\', \'save_metabox\' , 10, 2 );
add_action( \'publish_\'.$cpt_public_slug, \'save_metabox\' , 10, 2 );
function adding_custom_meta_boxes(){
global $cpt_public_slug;
add_meta_box(\'topictimeout\', \'Time\', \'cpt_form_site_Render\', $cpt_public_slug, \'side\', \'high\');
}
function cpt_form_site_Render(){
global $post;
$post_meta = get_post_meta($post->ID);
// put here input or textarea
}
function save_metabox($post_id, $post){
global $post;
// when first publish and then save
if( isset( $_POST[\'save\'] ) || isset( $_POST[\'publish\'] ) ) {
// you will need to use a $_POST param and validate before saving
$meta_val = isset( $_POST[\'topictimeout\'] ) ? sanitize_text_field( $_POST[\'topictimeout\'] ) : \'\';
// the $meta_val would be a $_POST param from inner meta box form
update_post_meta($post_id, \'topictimeout\', $meta_val);
}
}
您可以将此输入放在那里,例如:
<input type="text" name="topictimeout" value="<?=$post_meta[\'topictimeout\'][0]; ?>"/> in minutes