2017-01-13 26 views
0

我已經創建了一個自定義的帖子類型,其中包含多個元框用於存儲數據並將其打印在前端。問題是,當我保存它只保存第一個元框值,並沒有存儲其他元框值。WordPress:CPT添加多個元框,僅保存第一個元框的值

我有一個函數,我所有的元框被創建,我不知道這是否是最好的做法,它可能是我的主要問題?

我應該創建另一個多功能讓它工作,或者我的問題是什麼?

Wordpress

function create_post_type_ledning() { 

    // define labels for custom post type 
    $labels = array(
     'name' => 'Ledning', 
     'singular_name' => 'Event', 
     'edit_item' => 'Redigera Ledning', 
     'view_item' => 'Visa Ledning', 
     'not_found' => 'Ingen Ledning hittad', 
     'not_found_in_trash' => 'Ingen Ledning hittad i papperskorgen', 
    ); 

    $args = array (
     'labels' => $labels, 
     'public' => true, 
     'supports' => array( 
      'title', 
      'thumbnail'), 
     'menu_icon' => 'dashicons-groups', 
    ); 

    register_post_type('Ledning', $args); 
} 

add_action('init', 'create_post_type_ledning'); 

function create_meta_box_ledning() { 

    add_meta_box(
     'ledning_meta_box_namn', 
     'Namn:', 
     'create_info_metabox_ledningnamn', 
     'Ledning', 
     'normal', 
     'default' 
    ); 

    add_meta_box(
     'ledning_meta_box_telefon', 
     'Telefon:', 
     'create_info_metabox_ledningtelefon', 
     'Ledning', 
     'normal', 
     'default' 
    ); 

    add_meta_box(
     'ledning_meta_box_mobil', 
     'Mobil:', 
     'create_info_metabox_ledningmobil', 
     'Ledning', 
     'normal', 
     'default' 
    ); 

    add_meta_box(
     'ledning_meta_box_mail', 
     'Mail:', 
     'create_info_metabox_ledningmail', 
     'Ledning', 
     'normal', 
     'default' 
    ); 
} 

add_action('add_meta_boxes', 'create_meta_box_ledning'); 


function ledning_meta_box_namn($post) { 
    echo 'Infon skrivs in här'; 
} 

function ledning_meta_box_telefon($post) { 
    echo 'Infon skrivs in här'; 
} 

function ledning_meta_box_mobil($post) { 
    echo 'Infon skrivs in här'; 
} 

function ledning_meta_box_mail($post) { 
    echo 'Infon skrivs in här'; 
} 

function create_info_metabox_ledningnamn($post) { 
     wp_nonce_field('bp_metabox_nonce', 'bp_nonce'); 
     $namn = get_post_meta($post->ID, 'namn', true); 

?>    
     <form action="" method="post"> 
      <p>Skriv in förnamn och efternamn</p> 
      <p> 

       <label for="namn"><strong>Namn</strong></label> 
       <input type="text" id="namn" name="namn" size="26" value="<?php echo esc_attr($namn); ?>" placeholder="T ex Karl Svensson" /> 
      </p> 
     </form> 
<?php 
} 

function create_info_metabox_ledningtelefon($post) { 
wp_nonce_field('bp_metabox_nonce', 'bp_nonce'); 
     $telefon = get_post_meta($post->ID, 'telefon', true); 

?>    
     <form action="" method="post"> 
      <p>Skriv in ditt telefonnummer:</p> 
      <p> 

       <label for="telefon"><strong>Telefon</strong></label> 
       <input type="text" id="telefon" name="telefon" size="26" value="<?php echo esc_attr($telefon); ?>" placeholder="T ex 070-6567004" /> 
      </p> 
     </form> 
<?php 
} 

function create_info_metabox_ledningmobil($post) { 
     wp_nonce_field('bp_metabox_nonce', 'bp_nonce'); 
     $mobil = get_post_meta($post->ID, 'mobil', true); 

?>    
     <form action="" method="post"> 
      <p>Skriv in ditt mobilnummer:</p> 
      <p> 

       <label for="mobil"><strong>Mobil</strong></label> 
       <input type="text" id="mobil" name="mobil" size="26" value="<?php echo esc_attr($mobil); ?>" placeholder="T ex 010-47455631" /> 
      </p> 
     </form> 
<?php 
} 

function create_info_metabox_ledningmail($post) { 
     wp_nonce_field('bp_metabox_nonce', 'bp_nonce'); 
     $mail = get_post_meta($post->ID, 'mail', true); 

?>    
     <form action="" method="post"> 
      <p>Skriv in ditt E-post:</p> 
      <p> 

       <label for="mail"><strong>Mail</strong></label> 
       <input type="text" id="mail" name="mail" size="26" value="<?php echo esc_attr($mail); ?>" placeholder="T ex [email protected]" /> 
      </p> 
     </form> 
<?php 
} 

function save_post_meta($post_id) { 
    if(!isset($_POST['bp_nonce']) || 
     !wp_verify_nonce($_POST['bp_nonce'], 
     'bp_metabox_nonce')) return; 

    if(isset($_POST['namn'])) { 
     update_post_meta($post_id, 'namn', $_POST['namn']); 
    } 
    if(isset($_POST['telefon'])) { 
     update_post_meta($post_id, 'telefon', $_POST['telefon']); 
    } 
    if(isset($_POST['mobil'])) { 
     update_post_meta($post_id, 'mobil', $_POST['mobil']); 
    } 
    if(isset($_POST['mail'])) { 
     update_post_meta($post_id, 'mail', $_POST['mail']); 
    }  

} 
add_action('save_post', 'save_post_meta'); 

回答

1

嘗試下面的代碼,將工作:

function create_post_type_ledning() { 

    // define labels for custom post type 
    $labels = array(
     'name' => 'Ledning', 
     'singular_name' => 'Event', 
     'edit_item' => 'Redigera Ledning', 
     'view_item' => 'Visa Ledning', 
     'not_found' => 'Ingen Ledning hittad', 
     'not_found_in_trash' => 'Ingen Ledning hittad i papperskorgen', 
    ); 

    $args = array (
     'labels' => $labels, 
     'public' => true, 
     'supports' => array( 
      'title', 
      'thumbnail'), 
     'menu_icon' => 'dashicons-groups', 
    ); 

    register_post_type('Ledning', $args); 
} 

add_action('init', 'create_post_type_ledning'); 

function create_meta_box_ledning() { 

    add_meta_box(
     'ledning_meta_box_namn', 
     'Namn:', 
     'create_info_metabox_ledningnamn', 
     'Ledning', 
     'normal', 
     'default' 
    ); 

    add_meta_box(
     'ledning_meta_box_telefon', 
     'Telefon:', 
     'create_info_metabox_ledningtelefon', 
     'Ledning', 
     'normal', 
     'default' 
    ); 

    add_meta_box(
     'ledning_meta_box_mobil', 
     'Mobil:', 
     'create_info_metabox_ledningmobil', 
     'Ledning', 
     'normal', 
     'default' 
    ); 

    add_meta_box(
     'ledning_meta_box_mail', 
     'Mail:', 
     'create_info_metabox_ledningmail', 
     'Ledning', 
     'normal', 
     'default' 
    ); 
} 

add_action('add_meta_boxes', 'create_meta_box_ledning'); 


function ledning_meta_box_namn($post) { 
    echo 'Infon skrivs in här'; 
} 

function ledning_meta_box_telefon($post) { 
    echo 'Infon skrivs in här'; 
} 

function ledning_meta_box_mobil($post) { 
    echo 'Infon skrivs in här'; 
} 

function ledning_meta_box_mail($post) { 
    echo 'Infon skrivs in här'; 
} 

function create_info_metabox_ledningnamn($post) { 
     wp_nonce_field('bp_metabox_nonce', 'bp_nonce'); 
     $namn = get_post_meta($post->ID, 'namn', true); 

?>    

      <p>Skriv in förnamn och efternamn</p> 
      <p> 

       <label for="namn"><strong>Namn</strong></label> 
       <input type="text" id="namn" name="namn" size="26" value="<?php echo esc_attr($namn); ?>" placeholder="T ex Karl Svensson" /> 
      </p> 

<?php 
} 

function create_info_metabox_ledningtelefon($post) { 
wp_nonce_field('bp_metabox_nonce', 'bp_nonce'); 
     $telefon = get_post_meta($post->ID, 'telefon', true); 

?>    

      <p>Skriv in ditt telefonnummer:</p> 
      <p> 

       <label for="telefon"><strong>Telefon</strong></label> 
       <input type="text" id="telefon" name="telefon" size="26" value="<?php echo esc_attr($telefon); ?>" placeholder="T ex 070-6567004" /> 
      </p> 

<?php 
} 

function create_info_metabox_ledningmobil($post) { 
     wp_nonce_field('bp_metabox_nonce', 'bp_nonce'); 
     $mobil = get_post_meta($post->ID, 'mobil', true); 

?>    

      <p>Skriv in ditt mobilnummer:</p> 
      <p> 

       <label for="mobil"><strong>Mobil</strong></label> 
       <input type="text" id="mobil" name="mobil" size="26" value="<?php echo esc_attr($mobil); ?>" placeholder="T ex 010-47455631" /> 
      </p> 

<?php 
} 

function create_info_metabox_ledningmail($post) { 
     wp_nonce_field('bp_metabox_nonce', 'bp_nonce'); 
     $mail = get_post_meta($post->ID, 'mail', true); 

?>    

      <p>Skriv in ditt E-post:</p> 
      <p> 

       <label for="mail"><strong>Mail</strong></label> 
       <input type="text" id="mail" name="mail" size="26" value="<?php echo esc_attr($mail); ?>" placeholder="T ex [email protected]" /> 
      </p> 

<?php 
} 

function save_post_meta($post_id) { 
    if(!isset($_POST['bp_nonce']) || 
     !wp_verify_nonce($_POST['bp_nonce'], 
     'bp_metabox_nonce')) return; 


    if(isset($_POST['namn'])) { 
     update_post_meta($post_id, 'namn', $_POST['namn']); 
    } 
    if(isset($_POST['telefon'])) { 
     update_post_meta($post_id, 'telefon', $_POST['telefon']); 
    } 
    if(isset($_POST['mobil'])) { 
     update_post_meta($post_id, 'mobil', $_POST['mobil']); 
    } 
    if(isset($_POST['mail'])) { 
     update_post_meta($post_id, 'mail', $_POST['mail']); 
    }  

} 
add_action('save_post', 'save_post_meta'); 
+0

哇,它的作品! :) 我做錯了什麼? – user3289402

+0

:)如果它起作用,則將答案標記爲接受。 –

+0

非常感謝你!爲什麼它沒有與form-tag一起工作,我認爲這是提交和保存值的正確方法? – user3289402

相關問題