在归档页面会显示博客最后更新日期,找了很久,没有找到实现方法,能找到的基本上都是单篇文章修改时间,后来换了英文关键词才找到一个:通过 $wpdb 查询获取 WordPress 博客最近更新(发布/修改文章)日期时间。
$last = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')");
$last = date('Y年n月j日', strtotime($last[0]->MAX_m));
echo '最后更新:'.$last;
最近看水煮鱼折腾 wp_cache_,想把这个「最近更新时间」加入 wp_cache key,这样修改文章/页面,相关缓存就会快速失效,然后更新……
上面那个代码稍稍折腾也能用,折腾好,测试了一下,可以用。不过还是不死心,按理,WordPress 可能自带相关函数。
换了关键词,果然找到了 get_lastpostdate,然后 get_lastpostmodified……功夫不负有心人。
get_lastpostdate:获取 WordPress 网站最新发布文章的时间。
get_lastpostmodified:获取 WordPress 网站最新发布/修改文章的时间。
比如下面测试代码:
echo 'get_lastpostdate:'.get_lastpostdate();
echo 'get_lastpostmodified:'.get_lastpostmodified();
会输出如下结果:
get_lastpostdate:2021-01-30 04:05:44.000000
get_lastpostmodified:2021-02-07 08:50:45.000000
加上 blog 参数,就会修正时区,输出当地时间:
echo date( 'Y年n月j日 H:i:s', strtotime( get_lastpostmodified('blog') ) );
输出:2021年2月7日 12:56:10
怎么结合 wp_cache?在需要的地方,参考下面代码加上 get_lastpostmodified 即可:
$custom_query = wp_cache_get( 'custom_query', 'custom_query' );
wp_cache_set( 'custom_query:'.md5(maybe_serialize(get_lastpostmodified())), $custom_query, 'custom_query', 3600);
//改成:
$custom_query = wp_cache_get( 'custom_query:'.md5(maybe_serialize(get_lastpostmodified())), 'custom_query' );
wp_cache_set( 'custom_query:'.md5(maybe_serialize(get_lastpostmodified())), $custom_query, 'custom_query', 3600);
via:小风车 - https://ae.mba/1615/
这个功能真的很实用!以后博客得要常常学“调用”才可以
@老张博客 折腾不止~
美国服务器,速度还这么快,厉害
@晓新哈特 貌似也看线路,有时候也很慢……
@老头 我这里 打开 1ms
@晓新哈特 网络爱国了吧?我这边爱国了也没有这么快啊。。。
@老头 联通 4G
@晓新哈特 ?据说新出的日本软银线路联通很快……不过我这个只是普通套餐。
@老头 能分享您的 go 插件吗?
@晓新哈特 不是插件,原始版本是这两个:https://cyhour.com/749/ 和 https://cyhour.com/133/
春节快乐,阖家安康
@响石潭 谢谢,同祝春节快乐,阖家安康。