2017-01-13 3 views
0

Ich habe einen benutzerdefinierten Beitragstyp mit mehreren Meta-Boxen zum Speichern von Daten und zum Ausdrucken dieser auf dem Front-End erstellt. Das Problem ist, dass wenn ich es speichere nur den ersten Meta-Box-Wert speichern und die anderen Meta-Box-Werte nicht speichern.WordPress: CPT Hinzufügen mehrerer Meta-Boxen, speichert nur den Wert der ersten Metabox

Ich habe eine Funktion, in der alle meine Metaboxen erstellt werden, und ich weiß nicht, ob dies die beste Vorgehensweise ist, und es könnte mein Hauptproblem sein?

Sollte ich eine andere Mehrfachfunktion erstellen, um es zum Laufen zu bringen oder was ist mein Problem?

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'); 

Antwort

1

Versuchen unter Code, der funktioniert:

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

Wow, es funktioniert! :) Was habe ich falsch gemacht? – user3289402

+0

:) Wenn es funktioniert, dann markieren Sie die Antwort als akzeptieren. –

+0

Vielen Dank an Sie! Warum hat es nicht mit dem Form-Tag funktioniert, ich dachte, es wäre der richtige Weg, Werte zu übermitteln und zu speichern? – user3289402

Verwandte Themen