我创建了一个带有多个选择项的元框,它从自定义帖子类型中获取其值。
add_action("add_meta_boxes", "palinsesto_box");
function palinsesto_box() {
add_meta_box("palinsesto-meta", "Speakers", "palinsesto_manager_meta_options", "palinsesto", "side");}
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',$value );
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;
}
}
这部分代码只返回一个值并显示单词数组或数字....你能帮帮我吗????