便宜VPS,VPS推荐
整理VPS云服务器优惠信息
Jtti 恒创科技 TOTHost 华纳云 搬瓦工 GigsGigsCloud UCloud

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显示文章最后更新时间

赞(1)
关注我们
未经允许不得转载:VPS GO » WordPress显示文章最后更新时间教程