我制作了一个带有多个选项的元数据箱,它从另一个自定义帖子获取其值,我会保存多个选择并在管理页面中显示它。 使用此代码,我几乎做到了但是: 多重选择项和保存功能只保存一个值,列功能只显示单词数组或内容....你可以帮助我。
这是代码
function palinsesto_manager_meta_options($post)
{
wp_nonce_field( 'radio_schedule', 'schedule_noncename' );
echo '<label for="speaker_id">';
_e("Speaker", 'speaker_id' );
echo '</label> ';
$args = array( 'post_type' => 'speaker');
$loop = new WP_Query( $args );
echo '<select name="speaker_id" id="speaker_id" multiple="multiple">';
foreach($loop->posts as $dj):
if($dj->ID == get_post_meta( $post->ID, 'speaker_id', true ))
{
$select = 'selected';
}else{
$select = '';
}
echo '<option value="'.$dj->ID.'" '.$select.'>'.$dj->post_title.'</option>';
endforeach;
echo '</select>';
echo '<p>Tieni premuto CTRL per selezionare più speakers</p>';
}
add_action('save_post', 'save_palinsesto_manager_meta_options');
function save_palinsesto_manager_meta_options($post_id)
{global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
//if you remove this the sky will fall on your head.
return;
}else{
if( isset( $_POST['speaker_id'] ) ) {
update_post_meta( $post_id,'speaker_id', esc_attr( $_POST['speaker_id'] ) );
}
}
}
add_filter('manage_palinsesto_posts_columns', 'columns_palinsesto');
function columns_palinsesto($old_columns)
{
$new_columns = array(
'cb' => '<input type="checkbox">',
'img' => 'Immagine',
'title' => __('Palinsesto'),
'conduce' => 'Conduce',
);
return array_merge( $new_columns, $old_columns );
}
add_action('manage_palinsesto_posts_custom_column', 'get_palinsesto_columns',
10, 2);
function get_palinsesto_columns($col, $post_id)
{global $post;
$conduce=get_post_meta( $post_id,'speaker_id', true );
switch($col) {
case 'img':
if(has_post_thumbnail($post_id)) {
echo get_the_post_thumbnail($post_id);
} else {
echo 'Nessuna immagine!';
}
break;
case 'conduce':
echo $conduce;
break;
}
}
这是元框部分......
add_action('init', 'palinsesto_manager');
function palinsesto_manager() {
$labels = array(
'name' => __('Palinsesto'),
'singular_name' => __('programma'),
'add_new' => __('Aggiungi Programma'),
'add_new_item' => __('Nuovo Programma'),
'edit_item' => __('Modifica Programma'),
'new_item' => __('Nuovo Programma'),
'all_items' => __('Palinsesto'),
'view_item' => __('Visualizza '),
'search_items' => __('Cerca '),
'not_found' => __('Programma non trovato'),
'not_found_in_trash' => __('Programma non trovato nel cestino'),
);
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'rewrite' => array('slug' => 'palinsesto'),
'publicly_queryable' => true,
'has_archive' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_icon' => get_stylesheet_directory_uri() . '/images/palinsestoic.png',
'menu_position' => 5,
'supports' => array(
'title',
'thumbnail'
),
);
register_post_type('palinsesto', $args);
}
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 150, 150, true );
add_image_size( 'cover', 260, 160 );
}
add_action("add_meta_boxes", "palinsesto_box");
function palinsesto_box() {
add_meta_box("palinsesto-meta", "Speakers", "palinsesto_manager_meta_options", "palinsesto", "side");}
答案 0 :(得分:2)
将名称属性从speaker_id更改为speaker_id []
更改此代码:
<select name="speaker_id" id="speaker_id" multiple="multiple">
要
<select name="speaker_id[]" id="speaker_id" multiple="multiple">
它将保存为数组