最近有人留言问文章页面中更新时间是怎么加上去的,他自己使用的主题只显示了发布时间。本文就分享下WordPress文章中显示最后更新时间的方法,不需要安装插件。
代码分享
WordPress的文章页面是single.php
这个文件渲染的,所以我们要修改这个文件的代码。
找到文件中显示文章更新时间的代码块,下文以DUX主题为例,分别获取post_time
和update_time
即可,用到了get_the_time
和get_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.changshaseo.net">推荐</a>发布于 <?php echo $post_time ?></span>
<?php
} else {
?>
<span class="item"><a href="https://www.changshaseo.net">推荐</a>发布于 <?php echo $post_time ?></span>
<span class="item">更新于 <?php echo $update_time ?></span>
<?php
}
?>