1
我做了一個多選項的metabox,從另一個自定義帖子的值,我會保存多個選擇,並顯示在管理頁面。 惠特這個代碼我幾乎做到了,但: 多選擇項和保存功能只保存一個值和列功能只顯示字數組或ID ....你能幫我。WordPress的保存多個選擇和自定義列錯誤
這是代碼
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;
}
}
這是meta框部....從speaker_id
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");}
你貼錯代碼。列部分是不相關的,meta盒部分丟失。提示:縮進代碼*使生活更輕鬆*。像這樣是不可讀的。對我來說沒問題,因爲我粘貼在我的編輯器中,並選擇「自動縮進」和presto:可讀代碼。 – brasofilo
metabox正在工作,選擇項目也是如此(從另一個自定義帖子中獲取它的值)唯一的問題是它只保存一個值,而管理員顯示的是word數組或id號 – zen
如果沒有正確的代碼來重現,則無法提供幫助該問題,請參閱:http://sscce.org – brasofilo