Mam następujący kod, który dodaje niestandardowy typ wiadomości do deski rozdzielczej administratora i pudełko zwyczaj meta do okna po EDIT:Dodawanie wielu meta-pudełka do niestandardowego typu postu w Wordpress
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']);
}
moje pytanie czy jeśli chcę dodać dodatkowe pole meta, jakie jest najlepsze podejście?
Próbowałem dodać kolejne wywołanie add_meta_box w funkcji admin_init, a także utworzyłem dodatkową funkcję wywołania zwrotnego dla tego html meta box, ale nic nie zostało wygenerowane na interfejsie. Wszelkie wskazówki byłyby świetne.
EDIT: Tak to jest, jak to zrobiłem na więcej niż jednym polu meta (to działa):
//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']);
}
Można też poprosić tego http: //wordpress.stackex change.com/ – Joren
Lub w [codereview.se], ponieważ jest to działający kod, który ma być zoptymalizowany. – brasofilo
Oddałem nagrodę, ponieważ nigdzie w Internecie nie ma dynamicznego podejścia do tego problemu. Zastanawiałem się, jakie jest optymalne rozwiązanie obejmujące wszystkie elementy formy. – Joren