【问题标题】:Gravity Forms & Advanced Custom Fields重力表格和高级自定义字段
【发布时间】:2014-01-28 08:59:18
【问题描述】:

我已成功创建下拉菜单,以从我在站点 http://albertson.staging.wpengine.com/seminars/ 创建的名为“日期”的高级自定义字段中自动填充适当的信息。

按照此处的说明进行操作:

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

我遇到的唯一问题是如何以“漂亮”的格式显示日期。可以看到日期全是数字(20140129),而不是 01/28/2014

为了在上面的研讨会部分中适当地显示日期(红色边框),我使用:

<?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;
}

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案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 );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 2019-09-26
      • 2013-06-29
      • 1970-01-01
      • 2013-07-14
      • 2014-02-23
      • 2019-12-05
      相关资源
      最近更新 更多