我创建了一个post metabox,它使用datepicker来选择日期值。我尽可能地添加了metabox并让datepicker工作,但除此之外,我还在努力将数据保存到帖子中。这是我函数中的代码。php:
//Add date picker css
function admin_styles() {
if ( \'events\' === get_current_screen()->id ) {
wp_enqueue_style( \'jquery-ui-smoothness\', // wrapped for brevity
\'//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css\', [], null );
}
}
add_action(\'admin_print_styles\', \'admin_styles\');
//Activate function
function admin_scripts() {
if ( \'events\' === get_current_screen()->id ) {
wp_enqueue_script( \'admin\', get_template_directory_uri() .\'/js/admin.js\', [ \'jquery-ui-datepicker\' ] );
}
}
add_action( \'admin_enqueue_scripts\', \'admin_scripts\' );
//Add date meta-boxes
function post_date_field($post) {
echo \'<input type="text" id="jquery-datepicker" name="entry_post_date" value="\' . get_post_meta( $post->ID, \'entry_post_date\', true ) . \'">\';
}
function post_date_meta_box() {
add_meta_box(\'entry_post_date\', \'Date\', \'post_date_field\', \'events\', \'side\', \'high\');
}
add_action(\'add_meta_boxes\', \'post_date_meta_box\');
//Save Data
function save_date_meta_box($post_id){
global $post;
if( $post->post_type == "events"){
if (isset($_POST)){
update_post_meta( $post_id, \'entry_post_date\', strip_tags( $_POST[\'entry_post_date\'] ) );
}
}
}
发布或更新帖子后,我不认为日期值已保存