でカスタムポストタイプに複数のメタボックスを追加:私の質問は私はポスト編集ウィンドウに管理ダッシュとカスタムメタボックスにカスタムポストタイプを追加し、次のコード持っているワードプレス
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に対して追加のコールバック関数を作成しましたが、フロントエンドで何も生成されませんでした。どのポインタも素晴らしいでしょう。
EDIT:これは(この作品)私は複数のメタボックスのためにそれをやった方法です:
//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']);
}
また、HTTPでこれを求めることができる://wordpress.stackexをchange.com/ – Joren
または[codereview.se]で、最適化を目指す作業コードです。 – brasofilo
私は賞金を出しましたが、ウェブ上のどこにもこの問題に対する動的なアプローチがないためです。私は、すべてのフォーム要素を組み込んだ最適なソリューションが何であるか疑問に思っていました。 – Joren