我创建了一个post metabox,它使用datepicker来选择日期值。我甚至添加了metabox并让datepicker开始工作,但除此之外,我还在努力将数据保存到post中。这是我在functions.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 '';
}
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'] ) );
}
}
}发布或更新帖子后,我认为日期值没有保存
发布于 2020-06-17 20:39:25
好吧傻我..。我错过了这句关键的台词:add_action( 'save_post', 'save_date_meta_box' );,它现在似乎保存了日期值
https://wordpress.stackexchange.com/questions/369251
复制相似问题