将多个日期添加到 Wordpress 中的自定义帖子类型 [英] Add multiple dates to custom post type in Wordpress

查看:30
本文介绍了将多个日期添加到 Wordpress 中的自定义帖子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个包含页面、帖子和事件(您可以登录多个日期)的 Wordpress 网站.我是 Wordpress 的新手,所以我一直在为这次活动寻找理想的解决方案.

I am building a Wordpress site with pages, posts and events (with multiple dates you can sign in). I am new to Wordpress so I was looking for ideal solution for this events.

我认为最好的解决方案是创建名为事件"的自定义帖子类型,然后单独处理.

I believe the best solution is to create custom post type called "Event" and then handle it separately.

但是我不确定如何为每个事件添加多个日期.我一直在阅读有关自定义分类法和帖子元数据的内容,但我经历的每个示例都像是创建自定义类别、标签或向帖子添加单个值.

I am not sure however how to add multiple dates to each event. I have been reading about custom taxonomies and post metadata but every example I went through was like creating a custom category, tag or adding a single value to the post.

对于将多个日期(不知道提前多少)添加到自定义帖子类型,您有什么建议?它是分类法还是发布元数据?是否有带有内置日期输入验证或日期选择器的日期类型的元数据?

What would you recommend for adding multiple dates (dont know how many in advance) to a custom post type? Is it taxonomy or post metadata? Is there a metadata of type date with build-in date input validation or datepicker?

谢谢!

推荐答案

实现这一目标的步骤:

  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://stackoverflow.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.

这篇关于将多个日期添加到 Wordpress 中的自定义帖子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆