[WordPress] ワードプレスのユーザー メタ ボックスのすべてのデータを保存しません。

スポンサーリンク

Question

私が、私のカスタムを 2 つの独立したメタ ボックス投稿タイプを追加するコードの下。もともと ‘証言’ メタ ボックスのみ、素晴らしい仕事をしました。’クレジット’ 管理ツールでうまく表示されてが、データベースに保存していないを加えた今?

誰も助けることができる、うまくいけば、それは何も大手

// Allow registering of custom meta boxes
add_action( 'add_meta_boxes', 'add_project_metaboxes' );

// Register the Project Meta Boxes
function add_project_metaboxes() {
    add_meta_box('oak_testimonial', 'Testimonial', 'oak_testimonial', 'project', 'normal', 'default');
    add_meta_box('oak_credits', 'Credits', 'oak_credits', 'project', 'side', 'default');
}

// Add Testimonial Metabox
function oak_testimonial() {
    global $post;

    // Noncename needed to verify where the data originated
    echo '';

    // Get the testimonial data if its already been entered
    $quote = get_post_meta($post->ID, '_quote', true);
    $name = get_post_meta($post->ID, '_name', true);
    $title = get_post_meta($post->ID, '_title', true);
    $company = get_post_meta($post->ID, '_company', true);

    // Echo out the field
    echo '

Name:

'; echo ''; echo '

Job Title:

'; echo ''; echo '

Company:

'; echo ''; echo '

Quote:

'; echo ''; } // Add Credits Metabox function oak_credits() { global $post; // Noncename needed to verify where the data originated echo ''; // Get the credits data if its already been entered $role1 = get_post_meta($post->ID, '_role1', true); $name1 = get_post_meta($post->ID, '_name1', true); $role2 = get_post_meta($post->ID, '_role2', true); $name2 = get_post_meta($post->ID, '_name2', true); $role3 = get_post_meta($post->ID, '_role3', true); $name3 = get_post_meta($post->ID, '_name3', true); // Echo out the field echo '

Role:

'; echo ''; echo '

Name:

'; echo ''; echo '

Role:

'; echo ''; echo '

Name:

'; echo ''; echo '

Role:

'; echo ''; echo '

Name:

'; echo ''; } // Save the Metabox Data function oak_save_project_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['testimonialmeta_noncename'], plugin_basename(__FILE__) )) { return $post->ID; } if ( !wp_verify_nonce( $_POST['creditsmeta_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. $testimonial_meta['_quote'] = $_POST['_quote']; $testimonial_meta['_name'] = $_POST['_name']; $testimonial_meta['_title'] = $_POST['_title']; $testimonial_meta['_company'] = $_POST['_company']; $credit_meta['_role1'] = $_POST['_role1']; $credit_meta['_name1'] = $_POST['_name1']; $credit_meta['_role2'] = $_POST['_role2']; $credit_meta['_name2'] = $_POST['_name2']; $credit_meta['_role3'] = $_POST['_role3']; $credit_meta['_name3'] = $_POST['_name3']; // Add values of $testimonial_meta as custom fields foreach ($testimonial_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 values of $credit_meta as custom fields foreach ($credit_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', 'oak_save_project_meta', 1, 2); // save the custom fields

答え

の「名前」、あなたの ‘oak_credits’ の metabox が間違っている:

// Add Credits Metabox
function oak_credits() {
    global $post;

    // Noncename needed to verify where the data originated
    echo '';

    // Get the credits data if its already been entered
    $role1 = get_post_meta($post->ID, '_role1', true);
    $name1 = get_post_meta($post->ID, '_name1', true);
    $role2 = get_post_meta($post->ID, '_role2', true);
    $name2 = get_post_meta($post->ID, '_name2', true);
    $role3 = get_post_meta($post->ID, '_role3', true);
    $name3 = get_post_meta($post->ID, '_name3', true);

   // Echo out the field
    echo '

Role:

'; echo ''; echo '

Name:

'; echo ''; echo '

Role:

'; echo ''; echo '

Name:

'; echo ''; echo '

Role:

'; echo ''; echo '

Name:

'; echo ''; }

答え

、name =”_name”を変更名 =”_title”それは複数回繰り返して

http://stackoverflow.com/questions/27839196/wordpress-custom-meta-box-not-saving-all-data

スポンサーリンク

Leave a Reply