在自定义 wordpress post 循环中使用时,高级自定义字段日期选择器数据会出错
Advanced Custom field datepicker data giving errors when used within custom wordpress post loop
我有自定义的 wordpress 首页,我想以这种格式显示事件日期,但这里只显示 post 日期..我想用相同的格式替换它的事件日期
这是我的首页代码
这里是活动 post 管理页面
这是高级自定义字段页面,我在其中添加了 events_field 元数据框字段以在每个事件下显示 post 管理中的文本编辑器
添加 acf 字段之前的事件循环
<?php
$events = new WP_Query( array(
'post_type' => 'event',
'posts_per_page' => 2
));
while($events->have_posts()){
$events->the_post();
?>
<div class="event-summary">
<a class="event-summary__date t-center" href="#">
<span class="event-summary__month"><?php the_time('M');?></span>
<span class="event-summary__day"><?php the_time('d');?></span>
</a>
<div class="event-summary__content">
<h5 class="event-summary__title headline headline--tiny"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h5>
<p><?php echo wp_trim_words(get_the_excerpt(),15);?> <a href="<?php the_permalink();?>" class="nu gray">Learn more</a></p>
</div>
</div>
<?php } wp_reset_postdata(); ?>
问题是我在 php DateTime 对象中添加高级自定义字段名称后显示错误
添加 acf 字段后的事件循环
<?php
$events = new WP_Query( array(
'post_type' => 'event',
'posts_per_page' => 2
));
while($events->have_posts()){
$events->the_post();
?>
<div class="event-summary">
<a class="event-summary__date t-center" href="#">
<span class="event-summary__month"><?php the_field('event_date');
// $eventDate = new DateTime(get_field('event_date'));
// echo $eventDate->format('M');
?></span>
<span class="event-summary__day"><?php the_time('d');?></span>
</a>
<div class="event-summary__content">
<h5 class="event-summary__title headline headline--tiny"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h5>
<p><?php echo wp_trim_words(get_the_excerpt(),15);?> <a href="<?php the_permalink();?>" class="nu gray">Learn more</a></p>
</div>
</div>
<?php } wp_reset_postdata(); ?>
这是我看到的错误
如果我只是在循环中添加这段代码,它会显示 acf 字段中的完整日期年月
the_field('event_date');
看截图
但是我需要更改日期格式以匹配主题,就像我 posted
的第一个屏幕截图图像一样
使用 EPB 解决方案后,它可以工作,但只显示月份编号,而不是我需要显示月份的前三个字母,如 JUN、JUL
这里是使用EPB码后的当前截图
这里是使用 EPB 解决方案后的新事件循环代码
<?php
$events = new WP_Query( array(
'post_type' => 'event',
'posts_per_page' => 2
));
while($events->have_posts()){
$events->the_post();
?>
<div class="event-summary">
<a class="event-summary__date t-center" href="#">
<span class="event-summary__month"><?php
$date_string = get_field('event_date');
$eventDate = DateTime::createFromFormat('d/m/Y', $date_string);
echo $eventDate->format('m');
?></span>
<span class="event-summary__day"><?php the_time('d');?></span>
</a>
<div class="event-summary__content">
<h5 class="event-summary__title headline headline--tiny"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h5>
<p><?php echo wp_trim_words(get_the_excerpt(),15);?> <a href="<?php the_permalink();?>" class="nu gray">Learn more</a></p>
</div>
</div>
<?php } wp_reset_postdata(); ?>
任何帮助将不胜感激,谢谢
问题出在您使用的日期格式上。该日期格式不明确,因此 DateTime
无法正确解析它。
您可以使用 preg_replace
将其更改为 ISO 格式,并在构造 DateTime 对象时使用它。例如:
$iso = preg_replace('/([\d]{2})\/([\d]{2})\/([\d]{4})/', '--', get_field('event_date'));
$dt = new DateTime($iso);
编辑:或者,更好的是,使用 DateTime::createFromFormat
方法格式,让您明确说明输入格式。
$dt = DateTime::createFromFormat('d/m/Y', get_field('event_date'));
谢谢大家我去解决了
这是代码
<?php
$today = date('Ymd');
$events = new WP_Query( array(
'post_type' => 'event',
'posts_per_page' => 2,
'meta_key'=> 'event_date',
'orderby' =>'meta_value_num',
'order'=>'ASC',
'meta_query'=> array(
array(
'key'=>'event_date',
'compare'=>'>=',
'value'=>$today,
'type'=>'numeric'
)
)
));
while($events->have_posts()){
$events->the_post();
?>
<div class="event-summary">
<a class="event-summary__date t-center" href="#">
<span class="event-summary__month"><?php
$date_string = get_field('event_date');
$eventDate = DateTime::createFromFormat('d/m/Y', $date_string);
echo $eventDate->format('M');
?></span>
<span class="event-summary__day"><?php echo $eventDate->format('d');?></span>
</a>
<div class="event-summary__content">
<h5 class="event-summary__title headline headline--tiny"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h5>
<p><?php echo wp_trim_words(get_the_excerpt(),15);?> <a href="<?php the_permalink();?>" class="nu gray">Learn more</a></p>
</div>
</div>
<?php } wp_reset_postdata(); ?>
我有自定义的 wordpress 首页,我想以这种格式显示事件日期,但这里只显示 post 日期..我想用相同的格式替换它的事件日期
这是我的首页代码
这里是活动 post 管理页面
这是高级自定义字段页面,我在其中添加了 events_field 元数据框字段以在每个事件下显示 post 管理中的文本编辑器
添加 acf 字段之前的事件循环
<?php
$events = new WP_Query( array(
'post_type' => 'event',
'posts_per_page' => 2
));
while($events->have_posts()){
$events->the_post();
?>
<div class="event-summary">
<a class="event-summary__date t-center" href="#">
<span class="event-summary__month"><?php the_time('M');?></span>
<span class="event-summary__day"><?php the_time('d');?></span>
</a>
<div class="event-summary__content">
<h5 class="event-summary__title headline headline--tiny"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h5>
<p><?php echo wp_trim_words(get_the_excerpt(),15);?> <a href="<?php the_permalink();?>" class="nu gray">Learn more</a></p>
</div>
</div>
<?php } wp_reset_postdata(); ?>
问题是我在 php DateTime 对象中添加高级自定义字段名称后显示错误
添加 acf 字段后的事件循环
<?php
$events = new WP_Query( array(
'post_type' => 'event',
'posts_per_page' => 2
));
while($events->have_posts()){
$events->the_post();
?>
<div class="event-summary">
<a class="event-summary__date t-center" href="#">
<span class="event-summary__month"><?php the_field('event_date');
// $eventDate = new DateTime(get_field('event_date'));
// echo $eventDate->format('M');
?></span>
<span class="event-summary__day"><?php the_time('d');?></span>
</a>
<div class="event-summary__content">
<h5 class="event-summary__title headline headline--tiny"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h5>
<p><?php echo wp_trim_words(get_the_excerpt(),15);?> <a href="<?php the_permalink();?>" class="nu gray">Learn more</a></p>
</div>
</div>
<?php } wp_reset_postdata(); ?>
这是我看到的错误
如果我只是在循环中添加这段代码,它会显示 acf 字段中的完整日期年月
the_field('event_date');
看截图
使用 EPB 解决方案后,它可以工作,但只显示月份编号,而不是我需要显示月份的前三个字母,如 JUN、JUL
这里是使用EPB码后的当前截图
这里是使用 EPB 解决方案后的新事件循环代码
<?php
$events = new WP_Query( array(
'post_type' => 'event',
'posts_per_page' => 2
));
while($events->have_posts()){
$events->the_post();
?>
<div class="event-summary">
<a class="event-summary__date t-center" href="#">
<span class="event-summary__month"><?php
$date_string = get_field('event_date');
$eventDate = DateTime::createFromFormat('d/m/Y', $date_string);
echo $eventDate->format('m');
?></span>
<span class="event-summary__day"><?php the_time('d');?></span>
</a>
<div class="event-summary__content">
<h5 class="event-summary__title headline headline--tiny"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h5>
<p><?php echo wp_trim_words(get_the_excerpt(),15);?> <a href="<?php the_permalink();?>" class="nu gray">Learn more</a></p>
</div>
</div>
<?php } wp_reset_postdata(); ?>
任何帮助将不胜感激,谢谢
问题出在您使用的日期格式上。该日期格式不明确,因此 DateTime
无法正确解析它。
您可以使用 preg_replace
将其更改为 ISO 格式,并在构造 DateTime 对象时使用它。例如:
$iso = preg_replace('/([\d]{2})\/([\d]{2})\/([\d]{4})/', '--', get_field('event_date'));
$dt = new DateTime($iso);
编辑:或者,更好的是,使用 DateTime::createFromFormat
方法格式,让您明确说明输入格式。
$dt = DateTime::createFromFormat('d/m/Y', get_field('event_date'));
谢谢大家我去解决了
这是代码
<?php
$today = date('Ymd');
$events = new WP_Query( array(
'post_type' => 'event',
'posts_per_page' => 2,
'meta_key'=> 'event_date',
'orderby' =>'meta_value_num',
'order'=>'ASC',
'meta_query'=> array(
array(
'key'=>'event_date',
'compare'=>'>=',
'value'=>$today,
'type'=>'numeric'
)
)
));
while($events->have_posts()){
$events->the_post();
?>
<div class="event-summary">
<a class="event-summary__date t-center" href="#">
<span class="event-summary__month"><?php
$date_string = get_field('event_date');
$eventDate = DateTime::createFromFormat('d/m/Y', $date_string);
echo $eventDate->format('M');
?></span>
<span class="event-summary__day"><?php echo $eventDate->format('d');?></span>
</a>
<div class="event-summary__content">
<h5 class="event-summary__title headline headline--tiny"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h5>
<p><?php echo wp_trim_words(get_the_excerpt(),15);?> <a href="<?php the_permalink();?>" class="nu gray">Learn more</a></p>
</div>
</div>
<?php } wp_reset_postdata(); ?>