本文介绍的“历史上的今天”并非显示历史上的今天发生国家以及朝代的大事,而是针对于自己的网站,在去年的今天你发不步过的文章内容的一个展示,所以使用本功能非常适合老站互动,至少也得一年吧,下面说下代码有需要安装的站长可以尝试安装代码
// 历史上的今天
// https://www.waizhuti.com/2705.html
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(); ?>
即可
评论前必须登录!
注册