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

我们看到有些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页面模板中。

1.3/5 – (3 votes)

© 版权声明
THE END
喜欢就支持一下吧
点赞361 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容