WordPress显示文章最后更新时间教程

最近有人留言问VPS GO文章页面中更新时间是怎么加上去的,他自己使用的主题只显示了发布时间。本文就分享下WordPress文章中显示最后更新时间的方法,不需要安装插件。

一、代码分享

WordPress的文章页面是single.php这个文件渲染的,所以我们要修改这个文件的代码。

找到文件中显示文章更新时间的代码块,下文以DUX主题为例,分别获取post_timeupdate_time即可,用到了get_the_timeget_the_modified_time这两个函数:

<?php 
$post_time = get_the_time('Y-m-d');
$update_time = get_the_modified_time('Y-m-d');
function dateBDate($date1, $date2) {
    $date1_s = strtotime($date1);
    $date2_s = strtotime($date2);
    if ($date2_s - $date1_s > 0) {
        return true;
    } else {
        return false;
    }
} 
if (!dateBDate($post_time, $update_time)) {
    ?>
    <span class="item"><a href="https://www.vpsgo.com">VPS推荐</a>发布于 <?php echo $post_time ?></span>
<?php
} else {
    ?>
<span class="item"><a href="https://www.vpsgo.com">VPS推荐</a>发布于 <?php echo $post_time ?></span>
                <span class="item">更新于 <?php echo $update_time ?></span>
    <?php
}
?>

二、效果展示

效果就跟本站一样,会在文章页面显示发布时间和更新时间:

WordPress显示文章最后更新时间


【AD】DMIT美国洛杉矶Pro VPS套餐,$14.9/月起,1G-10Gbps带宽/CN2 GIA线路/有高防GIA可选
【AD】美国洛杉矶CN2 VPS/香港CN2 VPS/日本CN2 VPS推荐,延迟低、稳定性高、免费备份_搬瓦工vps