且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将多个日期添加到 Wordpress 中的自定义帖子类型

更新时间:2021-10-26 22:43:18

实现这一目标的步骤:

  1. 添加元框
  2. 添加多选日期选择器
  3. 保存元值
  4. 获取元值

现在,详细

添加元框&在其 HTML 中设置多选日期选择器.使用 jQuery 日期选择器多选和取消选择 添加多选日期选择器.

Add Meta Box & Set Multi Select Datepicker in its HTML. Use jQuery date picker multi select and unselect to add multi select date picker.

function myplugin_add_meta_box() {

    add_meta_box(
        'event-date',
        'Set Event Dates',
        'myplugin_meta_box_callback',
        'event'
    );
}
function myplugin_meta_box_callback(){
    // Set HTML here which you want to see under this meta box.
    // Refer https://***.com/questions/17651766/jquery-date-picker-multi-select-and-unselect
    // for multiselect datebox.
   echo '<input type="date" id="datePick" name=save-dates/>';
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );

保存元值

function myplugin_save_postdata($post_id){
    if ( 'event' == $_POST['post_type'] ) {
        update_post_meta($post_id, 'save-dates', sanitize_text_field( $_REQUEST['save-dates'] ));
    }
}
add_action( 'save_post', 'myplugin_save_postdata' );

获取元值

现在,无论您想在何处显示这些日期.只需从数据库中检索它们并开始使用它们.get_post_meta($post_id, 'save-dates',true);

不要忘记在使用它们之前explode(',', get_post_meta($post_id, 'save-dates',true)).由于日期以逗号格式保存.

Don't forget to explode(',', get_post_meta($post_id, 'save-dates',true)) before using them. As dates are being saved in comma format.