Quantcast
Channel: 暢想資源 » WordPress文章
Viewing all articles
Browse latest Browse all 2

於WordPress文章內添加「歷史上的今天」功能

0
0

當我們的WordPress部落格歷史越來越悠久後,我們就會發現,在N年前的今天(也就是該篇文章所發表日期),我們也曾經發表了文章。而今天暢想資源就來教大家如何於WordPress文章內添加上「歷史上的今天」功能,讓站長和訪客一同體會網站歷史!

預覽

PS:本文下方其實就有哦~ :laugh:

wordpress-history-post-demo

外掛(插件)版

純代碼教學

將下方代碼添加到「佈景函式庫 (functions.php)」的最底部 ?> 之前,然後點擊 更新檔案 即可~ :razz:

提示:你可以將下方代碼中高亮的兩行調整為你所想要輸出的標題及內容等! :razz:

/* 歷史上的今天開始(由AREFLY.COM製作) */
function wp_history_post_base($post_year, $post_month, $post_day){
	global $wpdb;
	$limit = 30;
	$order = "latest";
	if($order == "latest"){ $order = "DESC";} else { $order = '';}
	$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 $order limit $limit";
	$histtory_post = $wpdb->get_results($sql);
	return $histtory_post;
}

function wp_history_post_single(){
	$wp_history_post_content_list = '<p>%YEAR%年:<a href="%LINK%" title="%TITLE%" rel="external nofollow">%TITLE%(%COMMENTS_NUM%條評論)</a></p>';
	$wp_history_post_content_title = '<h3>歷史上的今天</h3>';
	$histtory_post = wp_history_post_base(get_the_time('Y'), get_the_time('m'), get_the_time('j'));
	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 .= $wp_history_post_content_list;
			$h_post = str_replace("%YEAR%", $h_year, $h_post);
			$h_post = str_replace("%LINK%", $h_permalink, $h_post);
			$h_post = str_replace("%TITLE%", $h_post_title, $h_post);
			$h_post = str_replace("%COMMENTS_NUM%", $h_comments, $h_post);
		}
	}
	if($h_post){
		$result = $wp_history_post_content_title.$h_post;
	}
	return $result;
	wp_reset_query();
}

function wp_history_post_content($content){
	global $wpdb;
	if(is_single()){
		$content .= wp_history_post_single();
	}
	return $content;
}
add_action('the_content', 'wp_history_post_content');
/* 歷史上的今天結束(由AREFLY.COM製作) */


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images