我有以下代码,为我的自定义帖子类型添加两个单独的元框。最初我只有“见证”和“#39;元盒,工作得很好。我现在已经添加了'点数'这在管理员中显示正常,但似乎没有保存到数据库?
任何人都可以提供帮助,希望它没什么大不了的。
// Allow registering of custom meta boxes
add_action( 'add_meta_boxes', 'add_project_metaboxes' );
// Register the Project Meta Boxes
function add_project_metaboxes() {
add_meta_box('oak_testimonial', 'Testimonial', 'oak_testimonial', 'project', 'normal', 'default');
add_meta_box('oak_credits', 'Credits', 'oak_credits', 'project', 'side', 'default');
}
// Add Testimonial Metabox
function oak_testimonial() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="testimonialmeta_noncename" id="testimonialmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the testimonial data if its already been entered
$quote = get_post_meta($post->ID, '_quote', true);
$name = get_post_meta($post->ID, '_name', true);
$title = get_post_meta($post->ID, '_title', true);
$company = get_post_meta($post->ID, '_company', true);
// Echo out the field
echo '<p>Name:</p>';
echo '<input type="text" name="_name" value="' . $name . '" class="widefat" />';
echo '<p>Job Title:</p>';
echo '<input type="text" name="_title" value="' . $title . '" class="widefat" />';
echo '<p>Company:</p>';
echo '<input type="text" name="_company" value="' . $company . '" class="widefat" />';
echo '<p>Quote:</p>';
echo '<input type="text" name="_quote" value="' . $quote . '" class="widefat" />';
}
// Add Credits Metabox
function oak_credits() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="creditsmeta_noncename" id="creditsmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the credits data if its already been entered
$role1 = get_post_meta($post->ID, '_role1', true);
$name1 = get_post_meta($post->ID, '_name1', true);
$role2 = get_post_meta($post->ID, '_role2', true);
$name2 = get_post_meta($post->ID, '_name2', true);
$role3 = get_post_meta($post->ID, '_role3', true);
$name3 = get_post_meta($post->ID, '_name3', true);
// Echo out the field
echo '<p>Role:</p>';
echo '<input type="text" name="_name" value="' . $role1 . '" class="widefat" />';
echo '<p>Name:</p>';
echo '<input type="text" name="_title" value="' . $name1 . '" class="widefat" />';
echo '<p>Role:</p>';
echo '<input type="text" name="_name" value="' . $role2 . '" class="widefat" />';
echo '<p>Name:</p>';
echo '<input type="text" name="_title" value="' . $name2 . '" class="widefat" />';
echo '<p>Role:</p>';
echo '<input type="text" name="_name" value="' . $role3 . '" class="widefat" />';
echo '<p>Name:</p>';
echo '<input type="text" name="_title" value="' . $name3 . '" class="widefat" />';
}
// Save the Metabox Data
function oak_save_project_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['testimonialmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !wp_verify_nonce( $_POST['creditsmeta_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.
$testimonial_meta['_quote'] = $_POST['_quote'];
$testimonial_meta['_name'] = $_POST['_name'];
$testimonial_meta['_title'] = $_POST['_title'];
$testimonial_meta['_company'] = $_POST['_company'];
$credit_meta['_role1'] = $_POST['_role1'];
$credit_meta['_name1'] = $_POST['_name1'];
$credit_meta['_role2'] = $_POST['_role2'];
$credit_meta['_name2'] = $_POST['_name2'];
$credit_meta['_role3'] = $_POST['_role3'];
$credit_meta['_name3'] = $_POST['_name3'];
// Add values of $testimonial_meta as custom fields
foreach ($testimonial_meta as $key => $value) { // Cycle through the $testimonial_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
}
// Add values of $credit_meta as custom fields
foreach ($credit_meta as $key => $value) { // Cycle through the $testimonial_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
}
}
add_action('save_post', 'oak_save_project_meta', 1, 2); // save the custom fields
答案 0 :(得分:0)
&#34;名称&#34;你的&#39; oak_credits&#39; metabox错了:
// Add Credits Metabox
function oak_credits() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="creditsmeta_noncename" id="creditsmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the credits data if its already been entered
$role1 = get_post_meta($post->ID, '_role1', true);
$name1 = get_post_meta($post->ID, '_name1', true);
$role2 = get_post_meta($post->ID, '_role2', true);
$name2 = get_post_meta($post->ID, '_name2', true);
$role3 = get_post_meta($post->ID, '_role3', true);
$name3 = get_post_meta($post->ID, '_name3', true);
// Echo out the field
echo '<p>Role:</p>';
echo '<input type="text" name="_role1" value="' . $role1 . '" class="widefat" />';
echo '<p>Name:</p>';
echo '<input type="text" name="_name1" value="' . $name1 . '" class="widefat" />';
echo '<p>Role:</p>';
echo '<input type="text" name="_role2" value="' . $role2 . '" class="widefat" />';
echo '<p>Name:</p>';
echo '<input type="text" name="_name2" value="' . $name2 . '" class="widefat" />';
echo '<p>Role:</p>';
echo '<input type="text" name="_role3" value="' . $role3 . '" class="widefat" />';
echo '<p>Name:</p>';
echo '<input type="text" name="_name3" value="' . $name3 . '" class="widefat" />';
}