カスタム投稿タイプから値を取得する複数選択項目を持つメタ ボックスを作成しました。
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;
}
}
コードのこの部分は 1 つの値のみを返し、単語の配列または数値を表示します....助けてもらえますか????