為了能更好地展示一些沒有更新的老文章,我們可以在文章頁面下添加一個“歷史上的今天”功能獲取老文章列表予以展示,如下圖所示:

該功能代碼取至柳城大佬的 wp-today 插件,具體實現方法將以下代碼添加到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);上述代碼默認是在文章結尾自動添加。
如果需要自定義顯示位置,則只需去掉上述代碼的以下部分。
function wp_today_auto($content){
if( is_single() ){
$content = $content.wp_today();
}
return $content;
}
add_filter('the_content', 'wp_today_auto',9999);然后在需要顯示的位置用下面函數調用即可。
<?php echo wp_today(); ?>
樣式文件的話沒有調整,大家根據自己喜好自行調整一下吧。






