我已经设置了WP自定义帖子类型(食谱)。我从一些在线教程中将代码拼凑在一起。代码(见下文)都很好。我还添加了一个自定义的meta框(如下),这似乎也可以。
但是,我需要在此CPT中再添加3个meta框,因此,我一步一步地添加了另一个meta框。这将显示在“添加新配方”管理屏幕中,但不会保存添加到该元框中的数据。我已经仔细检查了十遍代码,但无法正常工作。
希望有人可以指出我错了。
自定义帖子类型代码-工作:
// CREATE CUSTOM POST TYPE RECIPES
function ks_create_recipes_post_type(){
$labels = array(
'name' => __('Recipes'),
'singular_name' => __('Recipe'),
'add_new' => __('New Recipe'),
'add_new_item' => __( 'Add New Recipe'),
'edit_item' => __( 'Edit Recipe'),
'new_item' => __( 'New Recipe'),
'view_item' => __( 'View Recipe' ),
'search_items' => __( 'Search Recipes' ),
'not_found' => __( 'No Recipes Found' ),
'not_found_in_trash' =>
__( 'No Recipes found in Trash' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail',
'page-attributes'
),
'taxonomies' => array( 'post_tag', 'category'),
);
register_post_type( 'Recipes', $args );
}
add_action('init', 'ks_create_recipes_post_type');
add_action( 'init', 'ks_define_cuisine_type_taxonomy' );
function ks_define_cuisine_type_taxonomy() {
$labels = array(
'name' => 'Cuisine',
'singular_name' => 'Cuisines',
'search_items' => 'Search Cuisines',
'all_items' => 'All Cuisines',
'parent_item' => 'Parent Cuisine',
'parent_item_colon' => 'Parent Cuisine:',
'edit_item' => 'Edit Cuisines',
'update_item' => 'Update Cuisine',
'add_new_item' => 'Add New Cuisine',
'new_item_name' => 'New Cuisine Name',
'menu_name' => 'Cuisines',
'view_item' => 'View Cuisines'
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'query_var' => true,
'rewrite' => true
);
register_taxonomy( 'cuisine', 'recipes', $args );
}
第一个元框(文本字段)的代码-工作:
// add custom meta box - Number of SERVES per recipe
add_action( 'add_meta_boxes','ks_add_meta_box_1' );
function ks_add_meta_box_1() {
add_meta_box(
'ks_serves_metabox',
__('Serves:'),
'ks_create_serves_metabox',
'recipes',
'normal',
'high' );
}
function ks_create_serves_metabox( $post ) { ?>
<form action="" method="post">
<?php // add nonce for security
wp_nonce_field( 'ks_metabox_nonce', 'ks_nonce' );
//retrive the metadata values if they exist
$ks_serves = get_post_meta( $post->ID, 'Serves', true ); ?>
<label for "ks_serves">How many serves?</label>
<input type="text" name="ks_serves" value="<?php echo esc_attr(
$ks_serves ); ?>" />
</form>
<?php }
// save the meta box data
add_action( 'save_post','ks_save_serves_meta' );
function ks_save_serves_meta( $post_id ) {
// if the nonce isn't verified, prevent saving
if( !isset( $_POST['ks_nonce'] ) || !wp_verify_nonce( $_POST['ks_nonce'],
'ks_metabox_nonce' ) ) return;
if ( isset( $_POST['ks_serves'] ) ) {
$new_serve_value = ( $_POST['ks_serves'] );
update_post_meta( $post_id ,'Serves' , $new_serve_value );
}
}
第二个meta框(文本字段)的代码-不起作用:
// add custom meta box - PREP TIME
add_action( 'add_meta_boxes','ks_add_meta_box_2' );
function ks_add_meta_box_2() {
add_meta_box(
'ks_preptime_metabox',
__('Prep Time:'),
'ks_create_preptime_metabox',
'recipes',
'normal',
'high' );
}
function ks_create_preptime_metabox( $post ) { ?>
<form action="" method="post">
<?php // add nonce for security
wp_nonce_field( 'ks_metabox2_nonce', 'ks_nonce_2' );
//retrive the metadata values if they exist
$ks_preptime = get_post_meta( $post->ID, 'Prep Time', true ); ?>
<label for "ks_preptime">Prep Time</label>
<input type="text" name="ks_preptime" value="<?php echo esc_attr(
$ks_preptime ); ?>" />
</form>
<?php }
// save the meta box data
add_action( 'save_post','ks_save_preptime_meta' );
function ks_save_preptime_meta( $post_id ) {
// if the nonce isn't verified, prevent saving
if( !isset( $_POST['ks_nonce_2'] ) || !wp_verify_nonce(
$_POST['ks_nonce_2'], 'ks_metabox2_nonce' ) ) return;
if ( isset( $_POST['ks_preptime'] ) ) {
$new_preptime_value = ( $_POST['ks_preptime'] );
update_post_meta( $post_id ,'Prep Time' , $new_preptime_value );
}
}
除第二个meta框外,其他所有内容似乎都可以正常运行。但是代码有什么问题呢?
谢谢。