第一步
在自己的WordPress主题中找到functions.php
,然后呢加入以下方法,
代码如下:
//~ 历史上的今天,代码来自柳城博主的 WP-Today 插件
function today_in_history() {
$title = "历史上的今天"; // 标题
$limit = 5; // 列表显示的数量,默认5条数据
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 $limit";
$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>$h_year: <a href='".$h_permalink."' title='查看: ".$h_post_title."'>$h_post_title <span>($h_comments 评)</span></a></li>";
}
}
if ( $h_post ){
$result = "<section class='history-in-today'><p><strong>".$title."</strong></p><div><ul>".$h_post."</ul></div></section>";
} else {
$result = "<section class='history-in-today'><p><strong>".$title."</strong></p><div>很遗憾~,历史上的今天未发表过文章</div></section>";
}
return $result;
}
第二部
在需要展示的地方(比如:我是加在了文章详情页的内容区底部),加入引用代码:
<!-- 历史上的今天 -->
<?php echo today_in_history(); ?>
然后就会看到效果了,如果曾经未发布过文章,会提示:“很遗憾~,历史上的今天未发表过文章”,反之则显示文章列表,样式自行调整吧.