给 WordPress 站点的文章页面添加最后一次更新时间

WordPress 显示最后更新时间代码

老规矩,在 functions.php 文件中添加以下代码:

//给 WordPress 站点的文章页面添加最后一次更新时间
function dujin_post_update( $content ) {
$u_time = get_the_time('U'); 
$u_modified_time = get_the_modified_time('U'); 
$custom_content = ''; 
if ($u_modified_time >= $u_time + 86400) {
$updated_date = get_the_modified_time('Y.m.d-H:s'); //这里设置时间显示格式,可自由调整。
$custom_content .= '<p class="dujin-update">本文最后更新于<code>'. $updated_date . '</code>,某些文章具有时效性,若有错误或已失效,请在下方留言或联系<a href="https://www.dujin.org/jingege"><b>缙哥哥</b></a>。</p>';  
} 
    $custom_content .= $content;
    return $custom_content;
}
add_filter( 'the_content', 'dujin_post_update' );

其中86400就是秒数,意思是超过1天才显示最后更新时间,这个你可以根据自己的需求去制定。

附上CSS

添加到你的主题的CSS样式文件中,或者另外弄个CSS文件,总之自己弄上去就完事儿了!

/* 给 WordPress 站点的文章页面添加最后一次更新时间 -
https://www.dujin.org/14521.html */ .dujin-update{padding:10px
20px;background-color:#FEEFB3;border-radius:6px;border:1px
solid;font-size:14px;text-align:left}

第二种教程 直接引用

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
}
?>

相关文章