我有以下代码将自定义帖子类型添加到管理破折号和自定义元框添加到帖子编辑窗口:
function teasers_custom_init() {
$labels = array(
'name' => 'Teasers',
'singular_name' => 'Teaser',
'add_new' => 'Add New',
'add_new_item' => 'Add New Teasers',
'edit_item' => 'Edit Teaser',
'new_item' => 'New Teaser',
'all_items' => 'All Teasers',
'view_item' => 'View Teaser',
'search_items' => 'Search Teasers',
'not_found' => 'No teasers found',
'not_found_in_trash' => 'No teasers found in Trash',
'parent_item_colon' => 'Parent Page',
'menu_name' => 'Teasers'
);
$args = array(
'labels' => $labels,
'description' => 'set slider panels with loop times',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'Teasers' ),
'capability_type' => 'page',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => 60,
'supports' => array( 'title', 'thumbnail', 'page-attributes'),
);
register_post_type( 'teasers', $args );
}
add_action( 'init', 'teasers_custom_init' );
//adding the meta box when the admin panel initialises
add_action("admin_init", "admin_init");
// this adds the save teaser function on save post
add_action('save_post', 'save_teaser');
function admin_init(){
add_meta_box('teaser_loop', 'Loop Time', 'loop_meta', 'teasers', 'normal', 'default');
}
// callback function of add meta box that displays the meta box in the post edit screen
function loop_meta($post, $args){
$teaser_loop = get_post_meta($post->ID, 'teaser_loop', true);
?>
<label>Teaser Loop: </label><input type="text" name="teaser_loop" value="<?php echo $teaser_loop; ?>" /><br/>
<?php
}
// saving the teaser
function save_teaser(){
global $post;
update_post_meta($post->ID, 'teaser_loop', $_POST['teaser_loop']);
}
我的问题是,如果我想添加一个额外的元框,最好的方法是什么?
我尝试在 admin_init 函数中添加另一个 add_meta_box 调用,并为此元框 html 创建了一个额外的回调函数,但前端没有生成任何内容。任何指针都会很棒。
编辑:所以这就是我为多个元框做的方式(这有效):
//adding the meta box when the admin panel initialises
add_action("admin_init", "admin_init");
// this adds the save teaser function on save post
add_action('save_post', 'save_teaser');
function admin_init(){
add_meta_box('teaser_loop', 'Loop Time', 'loop_meta_1', 'teasers', 'normal', 'default');
add_meta_box('teaser_link', 'Teaser Link', 'loop_meta_2', 'teasers', 'normal', 'default');
}
// back function of add meta box that displays the meta box in the post edit screen
function loop_meta_1($post, $args){
$teaser_loop = get_post_meta($post->ID, 'teaser_loop', true);
?>
<label>Teaser Loop: </label><input type="text" name="teaser_loop" value="<?php echo $teaser_loop; ?>" /><br/>
<?php
}
function loop_meta_2($post, $args){
$teaser_link = get_post_meta($post->ID, 'teaser_link', true);
?>
<label>Teaser Link: </label><input type="text" name="teaser_link" value="<?php echo $teaser_link; ?>" /><br/>
<?php
}
// saving the teaser
function save_teaser(){
global $post;
update_post_meta($post->ID, 'teaser_loop', $_POST['teaser_loop']);
update_post_meta($post->ID, 'teaser_link', $_POST['teaser_link']);
}