问题描述
我正在尝试保存我创建的3个元框的输入。这是下面的代码。我错过了什么,因为我只保存了smashing-post-class2 id 3次。
未保存的两个Meta框是smashing-post-class和smashing-post-class1。输入字段仅保存来自smashing-post-class2的输入。
/* Fire our Meta Box setup function on the post editor screen. */
add_action( 'add_Meta_Boxes','smashing_add_post_Meta_Boxes' );
add_action( 'save_post','smashing_save_post_class_Meta',10,2 );
/* Create one or more Meta Boxes to be displayed on the post editor screen. */
function smashing_add_post_Meta_Boxes() {
// sku,price,short description
add_Meta_Box(
'smashing-post-class',// Unique ID
esc_html__( 'SKU','SKU' ),// Title
'smashing_post_class_Meta_Box',// Callback function
'products',// Admin page (or post type)
'normal',// Context
'default' // Priority
);
add_Meta_Box(
'smashing-post-class1',// Unique ID
esc_html__( 'Price','Price' ),// Title
'smashing_post_class_Meta_Box',// Callback function
'products',// Admin page (or post type)
'normal',// Context
'default' // Priority
);
add_Meta_Box(
'smashing-post-classs2',// Unique ID
esc_html__( 'Short Description','Short Description' ),// Context
'default' // Priority
);
}
/* display the post Meta Box. */
function smashing_post_class_Meta_Box( $post ) { ?>
<?PHP wp_nonce_field( basename( __FILE__ ),'smashing_post_class_nonce' ); ?>
<p>
<label for="smashing-post-class"><?PHP _e( "Add Meta tag",'example' ); ?></label>
<br />
<input class="widefat" type="text" name="smashing-post-class" id="smashing-post-class" value="<?PHP echo esc_attr( get_post_meta( $post->ID,'smashing_post_class',true ) ); ?>" size="30" />
</p>
<?PHP }
/* Save the Meta Box’s post Metadata. */
function smashing_save_post_class_Meta( $post_id,$post ) {
/* Verify the nonce before proceeding. */
if ( !isset( $_POST['smashing_post_class_nonce'] ) || !wp_verify_nonce( $_POST['smashing_post_class_nonce'],basename( __FILE__ ) ) )
return $post_id;
/* Get the post type object. */
$post_type = get_post_type_object( $post->post_type );
/* Check if the current user has permission to edit the post. */
if ( !current_user_can( $post_type->cap->edit_post,$post_id ) )
return $post_id;
/* Get the posted data and sanitize it for use as an HTML class. */
$new_Meta_value = ( isset( $_POST['smashing-post-class'] ) ? sanitize_html_class( $_POST['smashing-post-class'] ) : ’ );
/* Get the Meta key. */
$Meta_key = 'smashing_post_class';
/* Get the Meta value of the custom field key. */
$Meta_value = get_post_meta( $post_id,$Meta_key,true );
/* If a new Meta value was added and there was no prevIoUs value,add it. */
if ( $new_Meta_value && '' == $Meta_value )
add_post_Meta( $post_id,$new_Meta_value,true );
/* If the new Meta value does not match the old value,update it. */
elseif ( $new_Meta_value && $new_Meta_value != $Meta_value )
update_post_Meta( $post_id,$new_Meta_value );
/* If there is no new Meta value but an old value exists,delete it. */
elseif ( '' == $new_Meta_value && $Meta_value )
delete_post_Meta( $post_id,$Meta_value );
}
解决方法
您必须为每个输入创建一个单独的元字段。 在您的示例中,您刚刚创建了一个输入并进行了更新。
您可以在'save_post'钩中尝试使用
update_post_meta( $post_id,'smashing_post_class_2',$_POST['smashing_post_class_2'] );
update_post_meta( $post_id,'smashing_post_class_3',$_POST['smashing_post_class_3'] );
update_post_meta()
函数将创建该字段(如果不存在),因此您无需创建它。