我有自定义帖子类型,车辆,我添加了两个自定义元框,其中包含以下代码(from the codex)。唯一的问题是,当我保存时,他们都得到第二个框的值。我已经搜索过但无法找到如何将它们视为不同的元值。
/**
* Adds a box to the main column on the Post and Page edit screens.
*/
function lm_add_meta_box() {
add_meta_box(
'lm_vehicles_capacity',
__( 'Capacity', 'lm_textdomain' ),
'lm_meta_box_callback1',
'vehicles',//$screen
'side',
'high'
);
add_meta_box(
'lm_vehicles_upselltext',
__( 'Upsell Text', 'lm_textdomain' ),
'lm_meta_box_callback2',
'vehicles',//$screen
'side',
'high'
);
}
add_action( 'add_meta_boxes', 'lm_add_meta_box' );
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function lm_meta_box_callback1( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'lm_meta_box', 'lm_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_lm_meta_value_key1', true );
echo '<label for="lm_new_field1">';
_e( 'Description for this field', 'lm_textdomain' );
echo '</label> ';
echo '<input type="text" id="lm_new_field1" name="lm_new_field1" value="' . esc_attr( $value ) . '" size="25" />';
}
function lm_meta_box_callback2( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'lm_meta_box', 'lm_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_lm_meta_value_key2', true );
echo '<label for="lm_new_field2">';
_e( 'Description for this field', 'lm_textdomain' );
echo '</label> ';
echo '<input type="text" id="lm_new_field2" name="lm_new_field2" value="' . esc_attr( $value ) . '" size="25" />';
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function lm_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['lm_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['lm_meta_box_nonce'], 'lm_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['lm_new_field1'] ) || ! isset( $_POST['lm_new_field2'] ) ) {
return;
}
// Sanitize user input.
$my_data1 = sanitize_text_field( $_POST['lm_new_field1'] );
$my_data2 = sanitize_text_field( $_POST['lm_new_field2'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_lm_meta_value_key1', $my_data1 );
update_post_meta( $post_id, '_lm_meta_value_key2', $my_data2 );
}
add_action( 'save_post', 'lm_save_meta_box_data' );
答案 0 :(得分:1)
好的,你的代码中有一些错误。我想你只是从代码中提取它并在顶部添加了一个新的文件:P。我还没有对它进行测试,但我认为所有相关的变化都已添加。仍然可能会进行一些小的调整。
<?php /**
* Adds a box to the main column on the Post and Page edit screens.
*/
function lm_add_meta_box() {
add_meta_box(
'lm_vehicles_capacity',
__( 'Capacity', 'lm_textdomain' ),
'lm_meta_box_callback1',
'vehicles',//$screen
'side',
'high'
);
add_meta_box(
'lm_vehicles_upselltext',
__( 'Upsell Text', 'lm_textdomain' ),
'lm_meta_box_callback2',
'vehicles',//$screen
'side',
'high'
);
}
add_action( 'add_meta_boxes', 'lm_add_meta_box' );
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function lm_meta_box_callback1( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'lm_meta_box', 'lm_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_lm_meta_value_key1', true );
echo '<label for="lm_new_field1">';
_e( 'Description for this field', 'lm_textdomain' );
echo '</label> ';
echo '<input type="text" id="lm_new_field1" name="lm_new_field1" value="' . esc_attr( $value ) . '" size="25" />';
}
function lm_meta_box_callback2( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'lm_meta_box', 'lm_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_lm_meta_value_key2', true );
echo '<label for="lm_new_field2">';
_e( 'Description for this field', 'lm_textdomain' );
echo '</label> ';
echo '<input type="text" id="lm_new_field2" name="lm_new_field2" value="' . esc_attr( $value ) . '" size="25" />';
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function lm_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['lm_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['lm_meta_box_nonce'], 'lm_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['lm_new_field1'] ) || ! isset( $_POST['lm_new_field2'] ) ) {
return;
}
// Sanitize user input.
$my_data1 = sanitize_text_field( $_POST['lm_new_field1'] );
$my_data2 = sanitize_text_field( $_POST['lm_new_field2'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_lm_meta_value_key1', $my_data1 );
update_post_meta( $post_id, '_lm_meta_value_key2', $my_data2 );
}
add_action( 'save_post', 'lm_save_meta_box_data' );
?>
答案 1 :(得分:0)
是add_meta_box的样子
add_meta_box( $id, $title, $callback, $screen, $context,
$priority, $callback_args );
你对两个metabox使用相同的回调,并且最后一个metabox值被用来改变它们的回调。它将起作用perefect