WordPress给文章添加显示更新时间
在当前WordPress主题模板下的“ functions.php”文件中,添加下面代码就可以实现
//在文章和页面结尾添加最后更新时间 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-d H:i' ); //这里设置时间显示格式,可自由调整。 $updated_date = get_the_modified_time( 'Y-m-d' ); //这里设置时间显示格式,可自由调整。 $custom_content .= '<div class="last-updated">本文最后更新于:' . $updated_date . ' </div>'; } // 在文章底部 // $content .= $custom_content; $content = $custom_content.$content; return $content; } add_filter('the_content', 'my_last_updated_date'); add_filter('auth_cookie_expiration', 'custom_cookie_expiration', 99, 3); function custom_cookie_expiration($expiration, $user_id = 0, $remember = true) { if($remember) { $expiration = 31536000; // 365天 } return $expiration; }
如果文章发布时间超过 24 小时(86400秒 就是 24 小时,具体间隔时间可自行修改),则在文章底部添加一行新时间的提示。
默认时在文章底部显示此信息,如果希望在文章页面顶部显示,只需将上面代码中第10行内容
- $content .= $custom_content;
修改成
- $content = $custom_content.$content;
样式代码
- /* WordPress-为文章添加最后更新时间 */
- .last-updated{padding:10px 20px;background-color:#ffffcc;border-radius:6px;border:1px solid;font-size:14px;text-align:left}