WordPress: CPT Adding Multiple Meta boxes, 只保存第一个metabox的值

WordPress: CPT Adding Multiple Meta boxes, only saves the value of the first metabox

我创建了一个带有多个元框的自定义 post 类型,用于存储数据并在前端打印这些数据。问题是当我保存它时只保存第一个元框值而不存储其他元框值。

我有一个功能,我的所有元框都是在其中创建的,我不知道这是否是最好的做法,这可能是我的主要问题吗?

我应该创建另一个复合函数来让它工作还是我的问题是什么?

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 mail@gmail.com" />
            </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');

尝试以下有效的代码:

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 mail@gmail.com" />
            </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');