老蒋有在前面的文章中体验到「WP Last Modified Info」插件可以实现编辑文章最新时间的更新提示。这样有助于有一些时效性内容的更新。这里,我们实际上还可以使用这种无插件的方式。

我们可以看到实现这样子的效果。在文章的最前面添加一个标记,可以自定义时间格式和说明。

// 无插件最新更新时间 https://www.itbulu.com/wpnoplugin-updatetime.html 
function itbulu_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="itbulu-noplugin-update">本文最后更新于<code>'. $updated_date . '
,文章可能有时效性,若有错误或失效,请联系老蒋。

';
}

$custom_content .= $content;
return $custom_content;

}
add_filter( 'the_content', 'itbulu_post_update' );


这个代码添加到当前主题 Functions.php 中,我们也可以对其文本进行微调。
.itbulu-noplugin-update{padding:15px 15px;background-color:hsla(0,0%,100%,.1);border-radius:5px;border:1px solid;font-size:14px;text-align:left}

如果有必要的话,我们也可以修改一下CSS样式。


扫描二维码,在手机上阅读!