1

我正在为 wordpress 制作服装元盒。麻烦的是wordpress似乎只保留/保存我在字段中输入的一些值..我也找不到模式..所以这里是代码:

<?php
function add_products_metaboxes() {
    add_meta_box('sra_product_info', 'Product Information', 'sra_products_info', 'product', 'side', 'default');
  }
  // The Productinfo Metabox
function sra_products_info() {
    //get access to the post object
    global $post;
    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="productmeta_noncename" id="productmeta_noncename" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    // Get the data from the field if its already been entered
    $name = get_post_meta($post->ID, '_name', true);
    $price = get_post_meta($post->ID, '_price', true);
    $includes = get_post_meta($post->ID, '_includes', true);
    $supports = get_post_meta($post->ID, '_supports', true);
    $version = get_post_meta($post->ID, '_version' , true);
    $extrainfo = get_post_meta($post->ID, '_extrainfo', true);
    // Echo out the form
   echo '<form>';
    echo '<label for="_name">Name</label>' . '<input type="text" name="_name" value="' . $name . '"/>';
    echo '<label for="_price">Price</label>' . '<input type="text" name="_price" value="' . $price . '"/>';
    echo '<label for="_includes">Includes</label> <textarea name="_includes" rows="4" cols="10">' . $includes . '</textarea>'; 
    echo '<label for="_supports">Supports</label> <input type="text" name="_supports" value="' . $supports . '"/>';
    echo '<label for="_version">Version</label>' . '<input type="text" name="_version" value="' . $version . '"/>';
    echo '<label for="_extrainfo">Extras</label> <textarea name="_extrainfo" rows="4" cols="10">' . $extrainfo . '</textarea>'; 
   echo '</form>';

}

// Save the Metabox Data
function sra_save_product_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['productmeta_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
    // check if the field exists in the posts array - if it does, then put cintent in $product_meta.
    // this code needs to be refactored!

if (isset($_POST['_name'])) {
    $product_meta['_name'] = $_POST['_name'];    
    }

 if (isset($_POST['_price'])) {
    $product_meta['_price'] = $_POST['_price'];    
    }

if (isset($_POST['_includes'])) {
    $product_meta['_includes'] = $_POST['_includes'];    
    }

if (isset($_POST['_supports'])) {
    $product_meta['_supports'] = $_POST['_supports'];    
    }

if (isset($_POST['_version'])) {
    $product_meta['_version'] = $_POST['_version'];    
    }

if (isset($_POST['_extrainfo'])) {
    $product_meta['_extrainfo'] = $_POST['_extrainfo'];    
    }


    // Add values of $prpduct_meta as custom fields
    foreach ($product_meta as $key => $value) { // Cycle through the $product_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 CSL (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', 'sra_save_product_meta', 1, 2); // save the custom fields

你看到任何明显的错误吗?我想我对这段代码中自己的错误有点盲目。

4

1 回答 1

0

一般来说,我建议为您的字段名称使用前缀。像这样的值_name有太多机会与其他地方具有相同名称的其他值发生冲突。使用诸如之类的东西_product_name。你可以试试吗?如果您的代码适用于页面,它应该会产生影响。为什么不将带有“true”的第四个参数添加到 add_post_meta() 和 update_post_meta() 的先前值?请参阅有关这些功能的 WordPress 法典:http: //codex.wordpress.org/Function_Reference/add_post_meta。所以它会去:

        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
        update_post_meta($post->ID, $key, $value, /***OLDVALUE***/ ); // See http://codex.wordpress.org/Function_Reference/update_post_meta for old value
    } else { // If the custom field doesn't have a value
        add_post_meta($post->ID, $key, $value, true);
    }

我认为很明显你与同名的元键有冲突。不是在页面中,而是在帖子中。因此,请尝试使用第 4 个参数来确保您引用的是唯一键(我的建议仍然是,使用明确的前缀与其他任何东西区分开来,无论是插件、核心等)

于 2012-11-14T14:17:21.680 回答