首页 > 解决方案 > PHP在前端显示时更改字段值

问题描述

字段名称“day”有 5 个值,它们是:“mon”“tue”“wed”“thur”或“fri”。

当我在前端回显这些时:

                <div class="listing-footer-left">

                    <?php $day = get_post_meta( get_the_ID(),'day', true );

                    echo $day; ?>

                </div>

我想将值“mon”显示为“Mondays”,将值“tue”显示为“tuesdays”等

请你让我知道这是怎么可能的。

非常感谢,马特

标签: phphtmlwordpressformsfield

解决方案


你可以像这样使用datestrtotime

https://3v4l.org/RlHkr

<?php
$day = get_post_meta( get_the_ID(),'day', true );
echo date('l', strtotime($day));
echo $day;
?>

编辑:这仅适用于单数,但我认为复数没有内置的区域设置感知解决方案 - 如果您只需要它为英语工作,您可以制作一个数组:

https://3v4l.org/L8Jr4

<?php

$arr = ['mon' => 'Mondays', 'tue' => 'Tuesdays']; // ...
$day = 'mon';
echo $arr[$day];

或使用上面的方法,只需附加一个s

https://3v4l.org/2gkvI

<?php
$day = get_post_meta( get_the_ID(),'day', true );
echo date('l\s', strtotime($day));
echo $day;
?>

参考:

http://php.net/manual/en/function.date.php

http://php.net/manual/en/function.strtotime.php


推荐阅读