Meta box not saving

时间:2016-05-15 作者:Smee

我正在跟踪Noel Tock\'s 关于制作事件CPT的教程,到目前为止,我能够显示自定义列和自定义元框。

现在的问题是,我无法将数据保存在元框中。我很困惑,因为我发现当我按下更新按钮时,这些值不会保留在字段上,也不会反映在自定义列上。代码与教程类似。

自定义列:

function events_custom_columns($column){
global $post;
$custom = get_post_custom();
switch($column){
    case \'event_col_type\':
            $eventcats = get_the_terms($post->ID, \'event_type\');
            $ev_cat_html = array();
            if($eventcats){
                foreach($eventcats as $eventcat)
                    array_push($ev_cat_html, $eventcat->name);
                    echo implode($ev_cat_html, \', \');
            }
            else {
                echo \'None\';
            }
    break;
    case \'event_date\':
            $start = $custom[\'event_start_date\'][0];
            $end = $custom[\'event_end_date\'][0];

            $startdate = date(\'F j, Y\', $start);
            $enddate = date(\'F j, Y\', $end);

            echo $startdate . \'<br><em>\' . $enddate . \'</em>\';
    break;
    case \'event_time\':

            $time_formtt = get_option(\'time_format\');
            $starttime = $custom[\'event_start_date\'][0];
            $endtime = $custom[\'event_end_date\'][0];

            $startT = date($time_formtt, $starttime);
            $endT = date($time_formtt, $endtime);

            echo $startT . \' - \' . $endT;

    break;
    case \'event_location\':
            echo $custom[\'event_location\'][0];
    break;
}
}

自定义元框:add\\u action(\'admin\\u init\',\'events\\u admin\\u init\');

function events_admin_init(){
    add_meta_box(\'event_meta\', \'Event Details\', \'event_details_meta\', \'events\', \'normal\', \'default\');
}

function event_details_meta(){
global $post;
$custom = get_post_custom($post->ID);

$meta_SDate = $custom[\'event_start_date\'][0];
$meta_EDate = $custom[\'event_end_date\'][0];
$meta_STime = $meta_SDate;
$meta_ETime = $meta_EDate;

$time_format = get_option(\'time_format\');

if($meta_SDate == null){
    $meta_SDate = time();
    $meta_EDate = $meta_SDate;
    $meta_STime = 0;
    $meta_ETime = 0;
}

$sd = date("D, M d, Y", $meta_SDate);
$ed = date("D, M d, Y", $meta_EDate);
$st = date($time_format, $meta_STime);
$et = date($time_format, $meta_ETime);

echo \'<input type = "hidden" name = "events_nonce1" id = "events_nonce1" value = "\' . wp_create_nonce(\'events_nonce1\') . \'>"\';

?>

    <ul style = "list-style-type: none;">
        <li><label>Start Date: </label><input name = "event_start_date" class = "dp_date" value = "<?php echo $sd; ?>"</li>
        <li><label>End Date: </label><input name = "event_end_date" class = "dp_date" value = "<?php echo $ed; ?>"</li>
        <li><label>Start Time: </label><input name = "event_start_time" value = "<?php echo $st; ?>"</li>
        <li><label>End Time: </label><input name = "event_end_time" value = "<?php echo $et; ?>"</li>
    </ul>

<?php
}
以下是保存功能:

add_action(\'save_post\', \'save_events_deets\');

function save_events_deets(){
   global $post;

   if(!wp_verify_nonce1($_POST[\'events_nonce1\'], \'events_nonce1\'))
   {    return $post -> ID;}

   if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
       return;

   if(!current_user_can(\'edit_post\', $post -> ID))
   {    return $post -> ID;}

   if(!isset($_POST[\'event_start_date\'])){
       return $post;
   }

   $updStartD = strtotime($_POST[\'event_start_date\'] . $_POST[\'event_start_time\']);
   update_post_meta($post -> ID, \'event_start_date\', $updStartD);


   if(!isset($_POST[\'event_end_date\'])){
       return $post;
   }

   $updEndD = strtotime($_POST[\'event_end_date\'] . $_POST[\'event_end_time\']);
   update_post_meta($post -> ID, \'event_end_date\', $updEndD);   
}

1 个回复
SO网友:majick

既然您使用的是单个值,那么尝试一下:

$meta_SDate = get_post_meta($post->ID,\'event_start_date\',true);
$meta_EDate = get_post_meta($post->ID,\'event_end_date\',true);
另请注意,以下行需要一个空格来分隔已发布的值,以便strtotime 可以正常工作:

$updStartD = strtotime(trim($_POST[\'event_start_date\']) . \' \' . trim($_POST[\'event_start_time\']));
$updEndD = strtotime(trim($_POST[\'event_end_date\']) . \' \' . trim($_POST[\'event_end_time\']));
我已添加trim 删除左右空白,以防万一。