我的自定义post metabox出现此错误。自定义post和metabox工作正常,但如果我已启用调试,并且在尝试保存任何其他页面或post时,我将收到一个错误:
注意:未定义的索引:在/Users/jay/site/wp-content/themes/mytheme/dogs-post-type中。php在线53
注意:未定义的索引:在/Users/jay/site/wp-content/themes/mytheme/dogs-post-type中。php在线53
警告:无法修改标题信息-标题已由/Users/jay/site/wp-content/themes/mytheme/dogs-post-type.php:53)中的/Users/jay/site/wp-includes/pluggable发送。php在线876
我的代码:
<?php
// Add new custom post for dogs
function example_dogs_custom_init() {
$args = array(
\'label\' => \'Dogs\',
\'public\' => true,
\'capability_type\' => \'post\',
\'hierarchical\' => true,
\'has_archive\' => true,
\'menu_position\' => 5,
\'supports\' => array(\'title\', \'editor\'),
\'rewrite\' => array(\'slug\' => \'pets/dogs\', \'with_front\' => false)
);
register_post_type( \'dogs\', $args );
}
add_action( \'init\', \'example_dogs_custom_init\' );
// METABOX breed
add_action("add_meta_boxes", \'test_add_post_meta_boxes_breed\');
function test_add_post_meta_boxes_breed() {
add_meta_box(
\'breed-meta\', // Unique ID
\'Breed\', // Title
\'breed_cb\', // Callback function
\'dogs\', // Admin page (or post type)
\'side\', // Context
\'default\' // Priority
);
}
function breed_cb() {
global $post;
$breed_input = get_post_meta($post->ID, \'breed_field\', true);
// unique identifier, name of hidden field
wp_nonce_field(__FILE__, \'at_nonce\');
?>
<label for="breed_field">Breed:</label>
<input type="text" name="breed_field" id="breed_field" value="<?php echo $breed_input; ?>" />
<br><span></span><?php
}
add_action(\'save_post\', function() {
global $post;
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) return;
// security check - nonce
// verify this came from the our screen and with proper authorizarion,
// because save_post can be triggered at other times
if ( $_POST && !wp_verify_nonce($_POST[\'at_nonce\'], __FILE__) ) {
return;
}
if ( isset($_POST[\'breed_field\']) ) {
update_post_meta($post->ID, \'breed_field\', $_POST[\'breed_field\']);
}
});
?>
nonce字段有问题,但我不知道是什么。如果有帮助的话,代码结构来自JeffreyWays的WordPress自定义帖子类型魔法课程。