要为页面或帖子添加选项框,您需要使用add_meta_boxes
行动
//Register Meta Box
function register_meta_box() {
add_meta_box(
\'meta-box-id\',
__( \'MetaBox Title\', \'text-domain\' ),
\'meta_box_callback\',
\'post\',
\'advanced\',
\'high\'
);
}
add_action( \'add_meta_boxes\', \'register_meta_box\');
//Add field
function meta_box_callback( $post_id ) {
$output = \'<label for="title_field">\'. esc_html__(\'Title Field\', \'text-domain\') .\'</label>\';
$title_field = get_post_meta( $post_id->ID, \'title_field\', true );
$output .= \'<input type="text" name="title_field" id="title_field" class="title_field" value="\'. esc_attr($title_field) .\'" />\';
echo $output;
}
// Save meta field
add_action(\'save_post\', \'save_meta_field\');
function save_meta_field($post_id){
// Check nonce, sanitize field
update_post_meta($post_id, \'title_field\', $_POST[\'title_field\']);
}
The
add_meta_box()
参数必须设置为符合您的需要(高级和“高”),当然还有字段名和您希望显示框的屏幕。
您将发现有关add_meta_boxes
here
希望有帮助