将下面的代码放到博客主题的function.php里即可自动实现上图的效果了:
- //历史上的今天,代码来自柳城博客的WP-Today插件
- //针对现实的时间
- function wp_today(){
- global $wpdb;
- $post_year = get_the_time('Y');
- $post_month = get_the_time('m');
- $post_day = get_the_time('j');
- $sql = "select ID, year(post_date_gmt) as h_year, post_title, comment_count FROM
- $wpdb->posts WHERE post_password = '' AND post_type = 'post' AND post_status = 'publish'
- AND year(post_date_gmt)!='$post_year' AND month(post_date_gmt)='$post_month' AND day(post_date_gmt)='$post_day'
- order by post_date_gmt DESC limit 5";
- $histtory_post = $wpdb->get_results($sql);
- if( $histtory_post ){
- foreach( $histtory_post as $post ){
- $h_year = $post->h_year;
- $h_post_title = $post->post_title;
- $h_permalink = get_permalink( $post->ID );
- $h_comments = $post->comment_count;
- $h_post .= "<li><strong>$h_year:</strong> <a href='".$h_permalink."' title='".$h_post_title."' target='_blank'>$h_post_title($h_comments)</a></li>";
- }
- }
- if ( $h_post ){
- $result = "<h2>历史上的今天:</h2><ul>".$h_post."</ul>";
- }
- return $result;
- }
- function wp_today_auto($content){
- if( is_single() ){
- $content = $content.wp_today();
- }
- return $content;
- }
- add_filter('the_content', 'wp_today_auto',9999);
上述代码的23行里的“历史上的今天”大家可以随意修改的,比如:历史文章推荐等等。并不见得一定要用“历史上的今天”。
本段代码出自柳城博客的WP-Today插件,再次特此感谢柳城博主!

我的微信
这是我的微信扫一扫
2018年8月10日 16:13 地板
怎么移植到thinkphp里
2018年6月12日 21:08 板凳
谢谢收藏了
2018年6月12日 23:15 1层
@忘记了回忆 一般我自己用上了的功能就顺便发表一篇文章,记录一下又可以分享给更多的人。
2018年6月12日 00:43 沙发
厉害了!这个很棒啊~
2018年6月12日 23:15 1层
@姜辰 其实这个也是偶然的时候看到的,然后保存了下来。说来其实有些鸡肋了,得至少一年后才能体现出来。还得天天更新呢。