租号玩代理申请
A-A+

简单代码实现WordPress显示文章更新时间

2023年10月07日 建站相关 暂无评论 阅读 49 次

我们看到有些WordPress网站内容页是有可以看到文章的发布和更新日期的。我们从SEO角度,时间戳对于内容的加权是有一些因素的,这里如果我们的网站没有更新时间戳可以手工加上。

<?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">发布于 <?php echo $post_time ?></span>
<?php
} else {
    ?>
<span class="item">发布于 <?php echo $post_time ?></span>
                <span class="item">更新于 <?php echo $update_time ?></span>
    <?php
}
?>

这里用到post_time和update_time即可,用到了get_the_time和get_the_modified_time这两个函数。我们可以根据需要添加到对应的single.php页面模板中。

京东空调大促销