多选元框

时间:2016-04-15 12:44:18

标签: wordpress multi-select meta-boxes

我添加了多选元框到自定义后期类型它看起来不错但只保存一个选择。如何更改以下代码以保存所有选定的选项。

add_action( 'add_meta_boxes', 'add_condition_metaboxes'); function add_scondition_metaboxes() {
add_meta_box('condition', 'Информация спикеров', 'wpt_condition_author', 'condition', 'side', 'default');} function wpt_scondition_author() {
global $post;

// Noncename needed to verify where the data originated
echo '<input type="hidden" name="conditionmeta_noncename" id="conditionmeta_noncename" value="' . 
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

// Get the author data if its already been entered

$my_dropdown = get_post_meta($post->ID, '_dappcf_i_dropdown', true);
// Echo out the field

    $posts = get_posts(array('post_type'=> 'speakers', 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1));

        //here you add the HTML of the dropdown you add something like

        echo '<p>Select the speaker: <select multiple="yes" name="_dappcf_i_dropdown" class="widefat"  style="width:170px">';
        foreach ($posts as $post) {
        echo '<option value="', $post->ID, '">'.$post->post_title.'</option>';          }
    echo '</select>'; } function wpt_save_condition_meta($post_id, $post) {

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['conditionmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}

// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
    return $post->ID;

// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.


$condition_meta['_dappcf_i_dropdown'] = $_POST['_dappcf_i_dropdown'];
// Add values of $testimonial_meta as custom fields

foreach ($condition_meta as $key => $value) { // Cycle through the $testimonial_meta array!
    if( $post->post_type == 'revision' ) return; // Don't store custom data twice
    $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
    if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
        update_post_meta($post->ID, $key, $value);
    } else { // If the custom field doesn't have a value
        add_post_meta($post->ID, $key, $value);
    }
    if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
} } add_action('save_post', 'wpt_save_condition_meta', 1, 2); // save the custom fields

从其他自定义帖子类型中查询选项,并且可以更改帖子的数量。

0 个答案:

没有答案