簡體   English   中英

Wordpress PHP如何添加和保存選擇下拉列表的值以自定義帖子類型

[英]Wordpress php how to add and save the values of a select dropdown to custom post type

我在wordpress中創建了一個自定義帖子類型ewithin稱為“演員”

在這種自定義帖子類型中,我添加了一些元框,管理員可以在其中添加有關每個演員的自定義數據。

我需要這些元框之一作為選擇輸入而不是文本輸入,但是我不知道如何保存值並顯示為選定狀態。

這是我當前的php代碼:

// Add the Actors Meta Boxes
function add_actor_metaboxes() {
    add_meta_box('actors_info', 'Actor Info', 'Nial_Actors::actors_info', 'actors', 'normal', 'default');
}

// The Actors Metabox
function actors_info() {
    global $post;

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

    // Get the location data if its already been entered
    $spotlight_url = get_post_meta($post->ID, '_spotlight_url', true);
    $actor_gender = get_post_meta($post->ID, '_actor_gender', true);
    $actor_age = get_post_meta($post->ID, '_actor_age', true);
    $actor_height = get_post_meta($post->ID, '_actor_height', true);
    $actor_weight = get_post_meta($post->ID, '_actor_weight', true);

    // Echo out the field
    echo '<p>Spotlight URL</p>';
    echo '<input type="text" name="_spotlight_url" value="' . $spotlight_url  . '" class="widefat" />';
    echo '<p>Gender</p>';

    echo '<select name="_actor_gender" id="actor_gender">';
    echo    '<option value="null" ' . selected( $actor_gender, 'null' ) . '>--</option>';
    echo    '<option value="male" ' . selected( $actor_gender, 'male' ) . '>Male</option>';
    echo    '<option value="female" ' . selected( $actor_gender, 'female' ) . '>Female</option>';
    echo '</select>';

    echo '<p>Age</p>';
        echo '<input type="text" name="_actor_age" value="' . $actor_age  . '" class="widefat" />';
    echo '<p>Height</p>';
        echo '<input type="text" name="_actor_height" value="' . $actor_height  . '" class="widefat" />';
    echo '<p>Weight</p>';
        echo '<input type="text" name="_actor_weight" value="' . $actor_weight  . '" class="widefat" />';

}

// Save the Metabox Data
function actor_info_save($post_id, $post) {

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

    }
    //if ( !wp_verify_nonce( $_POST['actorinfometa_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.

    $actor_meta['_spotlight_url'] = $_POST['_spotlight_url'];
    $actor_meta['_actor_gender'] = $_POST['_actor_gender'];
    $actor_meta['_actor_age'] = $_POST['_actor_age'];
    $actor_meta['_actor_height'] = $_POST['_actor_height'];
    $actor_meta['_actor_weight'] = $_POST['_actor_weight'];

    // Add values of $actor_meta as custom fields

    foreach ($actor_meta as $key => $value) { // Cycle through the $actor_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
    }

}

有什么明顯的我想念的地方嗎?-我希望“性別”選項成為下拉選擇輸入

好的,我測試了您的代碼,當您注釋掉隨機數時,它可以保存

if ( !isset($_POST['actorinfometa_noncename']) || !wp_verify_nonce( $_POST['actorinfometa_noncename'], basename(__FILE__) )) {
    return $post->ID;
}

因此,我的猜測是您的現時正在崩潰。

在您的actors_info()其更改為

echo '<input type="hidden" name="actorinfometa_noncename" id="actorinfometa_noncename" value="' .
wp_create_nonce( 'actor_nonce' ) . '" />';

並將保存隨機數更改為

if ( !isset($_POST['actorinfometa_noncename']) || !wp_verify_nonce( $_POST['actorinfometa_noncename'], 'actor_nonce' )) {
    return $post->ID;

為我工作。 希望這可以幫助。

還要確保您具有save_post操作。 要么

add_action( 'save_post', array( $this, 'actor_info_save' ), 10, 2 );

要么

add_action( 'save_post', 'Nial_Actors::actor_info_save', 10, 2 );

或針對非面向對象的代碼:

add_action( 'save_post', 'actor_info_save', 10, 2 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM