我正在尝试修改我的网站上显示日期的格式,我在此处遵循了本教程:https://www.advancedcustomfields.com/resources/date-picker/
但是,日期输出始终是当前日期,而不是日期选择器上指定的日期。
你能看出这有什么问题吗?感谢。
<?php
$loop = new WP_Query( array( 'post_type' => 'event', 'paged' => $paged ) );
if ( $loop->have_posts() ) : ?>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
// get raw date
$date = get_field('event_date');
// make date object
$date = new DateTime($date);
?>
<li>
<a class="event__title" href="<?php the_permalink() ?>"><?php echo get_the_title(); ?></a>
<span class="event__location"><?php the_field('event_location'); ?></span>
<span class="event__date"><?php echo $date->format('j M Y'); ?></span>
<a href="<?php the_permalink ?>">
Read more >
</a>
</li>
<?php endwhile; ?>
</ul>
<?php
endif;
wp_reset_postdata();
?>
答案 0 :(得分:0)
您的代码似乎很好。您错误的最可能原因是event_date
字段的返回格式。
如果我是对,enabling PHP errors应该显示如下错误:
致命错误:未捕获的异常&#39;异常&#39; with message&#39; DateTime :: __ construct():无法解析时间字符串
一种可能的解决方案(可能是最简单的解决方案)是将event_date
字段返回格式更改为DateTime::__construct()
能够解析的内容。例如:
Y-m-d
请参阅: