经常性的更新已有网页,但是wordpress的显示规则一般是按照发布日期进行排序,这样对于显示重要经常更新的文档很不友好,需要将其改成按照更新日期进行排序的方式。按照一下代码可以实现。
- 文章排序
修改function.php
// sort by modify time function order_posts_by_mod_date($orderby) { if (is_home() || is_archive() || is_feed()) { $orderby = "post_modified_gmt DESC"; } return $orderby; } add_filter('posts_orderby', 'order_posts_by_mod_date', 999);
- 显示最近更新日期和版权协议
方式一:修改代码(推荐)
在主题编辑器=>function.php,添加如下代码:
function wpb_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'); $custom_content .= '<p class="last-updated">Last updated on '. $updated_date .' CC BY-SA 4.0 </p>'; } $custom_content .= $content; return $custom_content; } add_filter( 'the_content', 'wpb_last_updated_date' );
在style.css增加如下代码:
.last-updated { color: #db7c22; background: #fff4b9; border: 1px solid #eac946; overflow: hidden; margin: 10px 0; padding: 15px 15px 15px 35px; font-size: 14px; }
效果如下:
方式二:安装Last Updated Shortcode这款插件
然后使用方法就是直接在写文章的时候插入短代码[lastupdated]就可以了,像下图这样。
效果如下图:
- 补充说明php关于时间格式的问题
参考资料:
- https://blog.csdn.net/qq_37640597/article/details/104771874
- https://blog.naibabiji.com/skill/wordpress-xian-shi-zui-hou-geng-xin-shi-jian.html
- https://www.php.net/manual/en/function.date.php
- https://www.php.net/manual/en/datetime.format.php
- https://wordpress.org/support/article/formatting-date-and-time/