又是新的一年,网站也运行了好些年了,差不多可以给网站加上历史上的今天的功能了。

代码

//历史上的今天,代码来自柳城博主的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>&nbsp;&nbsp;<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);

将代码加入到当前主题的Functions.php文件中。

调用

<?php echo wp_today(); ?>

在需要调出历史今天的位置加上上面调出代码。对于CSS样式我们自行设置添加。

参考资料

  1. wordpress插件发布:历史上的今天wp-today ·[博客·柳城·2010年3月31日]

往年的今天

  • 2020:  下沉市场(0)
  • 2019:  品牌设计入门导航(0)
  • 2018:  婚外恋(7)