如上图,参考微信公众号文章,在文章底部自动添加文章最新修改时间。
我比较讨厌网站后台装一大堆的插件,今天分享的是不使用插件如何实现这个功能。
实现方法
首先,我们打开主题的 function.php 文件,插入进去如下代码即可:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| /**
* @desc 文章最后更新时间/全局
* @param $content
* @return string
*/
function my_last_updated_date( $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-j h:s a' );
$custom_content .= '<p class="last-updated entry-meta">最后更新 ' . $updated_date . '</p>';
}
$custom_content .= $content;
return $custom_content;
}
add_filter( 'the_content', 'my_last_updated_date' );
|