2014-01-09 28 views
0

我已经成功创建了下拉菜单,以自动填充我在网站http://albertson.staging.wpengine.com/seminars/上创建的名为“日期”的高级自定义字段中的相应信息。重力形式和高级自定义字段

随后沿着这里的说明:

http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields

我遇到的唯一问题是如何显示在一个“漂亮”的格式显示。你可以看到日期是所有号码(20140129),而不是2014年1月28日

要在以上(境红色)研讨会的部分适当地显示日期我使用:

<?php if(get_field('date')): ?> 

<?php 
$date = get_field('date'); 
// $date = 19881123 (23/11/1988) 

// extract Y,M,D 
$y = substr($date, 0, 4); 
$m = substr($date, 4, 2); 
$d = substr($date, 6, 2); 

// create UNIX 
$time = strtotime("{$d}-{$m}-{$y}"); 

// format date (November 11th 1988) 
echo date('M d', $time); 
?> 

我如何在我创建的重力形式函数中传递这个相同的信息以获得很好的日期显示?以下是我迄今为止的Gravity Forms的功能。

add_filter('gform_pre_render_4', 'populate_dates'); 

function populate_dates($form){ 

    foreach($form['fields'] as &$field){ 

     if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-dates') === false) 
      continue; 

     // you can add additional parameters here to alter the posts that are retreieved 
     // more info: http://codex.wordpress.org/Template_Tags/get_posts 
     $currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y"))); 

     $events = get_posts(array(
        'post_type' => 'seminars', 
        'orderby' => 'date', 
        'order' => 'ASC', 
        'meta_query'=> array(
         array(
          'key' => 'date', 
          'compare' => '>=', 
          'value' => $currentdate, 
          'type' => 'DATE', 
         )), 
        'meta_key' => 'date', 
        )); 

     // update 'Select a Post' to whatever you'd like the instructive option to be 
     $choices = array(array('text' => 'Select a Date', 'value' => ' ')); 

     foreach($events as $post){ 
      $choices[] = array('text' => $post->date, 'value' => $post->date); 

     } 

     $field['choices'] = $choices; 

    } 

    return $form; 
} 

回答

1

听起来像您正在寻找PHP的date函数。我们通过strtotime()将日期字符串转换为时间戳,然后根据需要使用date()格式化日期。

$formatted_date = date('m/d/Y', strtotime($post->date)); 

在你的代码示例:

foreach($events as $post){ 
    $formatted_date = date('m/d/Y', strtotime($post->date)); 
    $choices[] = array('text' => $formatted_date, 'value' => $post->date); 
}