1

我制作了一个带有多个选择项的元框,该项目从另一个自定义帖子中获取其值,我将保存多项选择并将其显示在管理页面中。这段代码我几乎做到了,但是:多选项目和保存功能只保存一个值,而列功能只显示单词数组或id....你能帮帮我吗?

这是代码

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");}
4

1 回答 1

5

将名称属性从扬声器 ID 更改为扬声器 ID []

更改此代码:

<select name="speaker_id" id="speaker_id" multiple="multiple">

<select name="speaker_id[]" id="speaker_id" multiple="multiple">

它将被保存为数组

于 2015-03-01T15:36:05.623 回答