WordPress代码实现历史上的今天发布过的文章列表

第一步

在自己的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:&nbsp;&nbsp;<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(); ?>

然后就会看到效果了,如果曾经未发布过文章,会提示:“很遗憾~,历史上的今天未发表过文章”,反之则显示文章列表,样式自行调整吧.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值