我一步一步地使用Smashing Magazine教程。但是我的自定义字段中没有自动保存功能。单击“发布和更新”按钮后,字段为空。如何解决问题。
function my_custom_post_movie() {
$labels = array(
'name' => _x( 'movies', 'post type general name' ),
'singular_name' => _x( 'movie', 'post type singular name' ),
'add_new' => _x( 'Add New', 'book' ),
'add_new_item' => __( 'Add New movie' ),
'edit_item' => __( 'Edit movie' ),
'new_item' => __( 'New movie' ),
'all_items' => __( 'All movies' ),
'view_item' => __( 'View movie' ),
'search_items' => __( 'Search movies' ),
'not_found' => __( 'No movies found' ),
'not_found_in_trash' => __( 'No movies found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'movies'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our movies and movie specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive' => true,
);
register_post_type( 'movie', $args );
}
add_action( 'init', 'my_custom_post_movie' );
// Register Custom Taxonomies
function my_taxonomies_movie() {
$labels = array(
'name' => _x( 'movie Categories', 'taxonomy general name' ),
'singular_name' => _x( 'movie Category', 'taxonomy singular name' ),
'search_items' => __( 'Search movie Categories' ),
'all_items' => __( 'All movie Categories' ),
'parent_item' => __( 'Parent movie Category' ),
'parent_item_colon' => __( 'Parent movie Category:' ),
'edit_item' => __( 'Edit movie Category' ),
'update_item' => __( 'Update movie Category' ),
'add_new_item' => __( 'Add New movie Category' ),
'new_item_name' => __( 'New movie Category' ),
'menu_name' => __( 'movie Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'movie_category', 'movie', $args );
}
add_action( 'init', 'my_taxonomies_movie', 0 );
// Meta Box
add_action( 'add_meta_boxes', 'movie_date_box' );
function movie_date_box() {
add_meta_box(
'movie_date_box',
__( 'movie date', 'myplugin_textdomain' ),
'movie_date_box_content',
'movie',
'side',
'high'
);
}
function movie_date_box_content( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'movie_date_box_content_nonce' );
echo '<label for="movie_date"></label>';
echo '<input type="text" id="movie_date" name="movie_date" placeholder="enter a date" />';
}
add_action( 'save_post', 'movie_date_box_save' );
function movie_date_box_save( $post_id ) {
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( !wp_verify_nonce( $_POST['movie_date_box_content_nonce'], plugin_basename( __FILE__ ) ) )
return;
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return;
}
$movie_date = $_POST['movie_date'];
update_post_meta( $post_id, 'movie_date', $movie_date );
}
感谢您的帮助。
答案 0 :(得分:0)
您的信息实际上已保存到数据库中,但一旦提交了帖子,就不会调用该信息。
变化:
function movie_date_box_content( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'movie_date_box_content_nonce' );
echo '<label for="movie_date"></label>';
echo '<input type="text" id="movie_date" name="movie_date" placeholder="enter a date" />';
}
以下内容:
function movie_date_box_content( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'movie_date_box_content_nonce' );
echo '<label for="movie_date"></label>';
echo '<input type="text" id="movie_date" name="movie_date" placeholder="enter a date" value="' . get_post_meta( $post->ID, 'movie_date', true ) . '" />';
}
您错过了value="' . get_post_meta( $post->ID, 'movie_date', true ) . '"
get_post_meta()
从数据库中调用元键movie_date
并显示该键,如果该键中存储了任何内容。
同样改变:
if ( !wp_verify_nonce( $_POST['movie_date_box_content_nonce'], plugin_basename( __FILE__ ) ) )
return;
以下内容:
if ( !isset($_POST['movie_date_box_content_nonce']) || !wp_verify_nonce( $_POST['movie_date_box_content_nonce'], plugin_basename( __FILE__ ) ) )
return;
这将检查是否设置了nonce字段,而不是 NULL 。它会处理显示在管理员中的未声明索引通知。