当我们的WordPress博客历史越来越悠久后,我们就会发现,在N年前的今天(也就是该篇文章所发表日期),我们也曾经发表了文章。而今天畅想资源就来教大家如何于WordPress文章内添加上“历史上的今天”功能,让站长和访客一同体会网站历史!
预览
PS:本文下方其实就有哦~
插件(插件)版
纯代码教学
将下方代码添加到“布景函式库 (functions.php)”的最底部 ?>
之前,然后点击 即可~
提示:你可以将下方代码中高亮的两行调整为你所想要输出的标题及内容等!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
/* 历史上的今天开始(由AREFLY.COM制作) */ function wp_history_post_base($post_year, $post_month, $post_day){ global $wpdb; $limit = 30; $order = "latest"; if($order == "latest"){ $order = "DESC";} else { $order = '';} $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 $order limit $limit"; $histtory_post = $wpdb->get_results($sql); return $histtory_post; } function wp_history_post_single(){ $wp_history_post_content_list = '<p>%YEAR%年:<a href="%LINK%" title="%TITLE%" rel="external nofollow">%TITLE%(%COMMENTS_NUM%条评论)</a></p>'; $wp_history_post_content_title = '<h3>历史上的今天</h3>'; $histtory_post = wp_history_post_base(get_the_time('Y'), get_the_time('m'), get_the_time('j')); 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 .= $wp_history_post_content_list; $h_post = str_replace("%YEAR%", $h_year, $h_post); $h_post = str_replace("%LINK%", $h_permalink, $h_post); $h_post = str_replace("%TITLE%", $h_post_title, $h_post); $h_post = str_replace("%COMMENTS_NUM%", $h_comments, $h_post); } } if($h_post){ $result = $wp_history_post_content_title.$h_post; } return $result; wp_reset_query(); } function wp_history_post_content($content){ global $wpdb; if(is_single()){ $content .= wp_history_post_single(); } return $content; } add_action('the_content', 'wp_history_post_content'); /* 历史上的今天结束(由AREFLY.COM制作) */ |
历史上的今天
2013年:植物大战僵尸2限时免费啦(4条评论)
不知何岁月,得与尔同归?
学习了
手机不支持回复呀,
我以为是真的历史上的今天呢 晕!
请问有没有真正的历史上的今天的插件或代码?就是今天10月20号就显示历史上10月20号的内容?
我也尝试过找过,但是一直没找到类似的插件....

hhh,终于把这个教程放出来了。
早就有了啊.......

这个有意思!我喜欢,收了。
很好的功能,当然这个功能要有一定的文章数量作为支撑
我的博客已经使用了您的代码,很好的功能,谢谢您
是啊~不过慢慢的文章就会多起来了!

好文章!支持一下!
好评论!支持一下!
老文章不堪入目,因为数据死而复生几十次图片都死了
这个功能很人性化,我明天也安装了试试。PS:你那下载按钮是用什么插件?
那个下载按钮是使用的我自己开发的“Shortcode Button”插件~
坚持就是胜利~

靠,仔细看了一下才发现是调用站内同日文章啊,我之前的数据都删掉了,悲剧。
这个不错
正需要,把他搞到我网站的顶部。
东西是不错,可惜没满一年的博客就没有历史上的今天了吧
是啊~不过没有历史也不会显示的~
挺好,内容从哪来?
那不错的
可惜我的老文章数据早都没了...
慢慢重新开始吧!
呃......
怎么了?

不折腾~
QQ空间上就有这个1
不错哦,来支持了,话说博主还是用mac发博文么...目前我还是XP路过←.←
感谢支持~~Mac是非常好用的!

代码拿走 已经加上了
恋旧~
不错,不错,支持一个。我记得这个有个插件版!
前来支持,手机模板不错
哟哟哟,切克闹。。。