カレンダーへのリンク追加

tested WordPress3.8

<div><?php
/**
 * Template Name: Calendar test
 *
 * Event Calendar for WordPress3.8
 * base code:http://wpxtreme.jp/event-calendar-with-wordpress
 */
 
	$cache= false;// 実用にする場合は、trueがお勧め

	$result= get_transient( 'my_event_calendar_posts' );
	
	if ( $result !== false &amp;&amp; $cache== true ) {
	
		echo $result;
	
	}else{
	 
		global $wpdb;
		$link= '';
		
		$category_ids= '174'; //conma separated ids
		$output= get_calendar(true, false);

	
		if ( preg_match_all( '@<td([^>]*)><a href="' . home_url() . '/([^0-9]*)?([0-9/]+)?([^"]*)" title="[^"]*?">([0-9]+?)</a></td>@i', $output, $matches ) ) {
		
		/* $maches に、カレンダー内のリンクをスクレイピングして溜め込む*/
		
			foreach($matches[3] as $key=> $match){
		/* URL中の 数字と/で構成される	部分を日付だとみなして、値を取得*/		
				$date = preg_replace('@/@', '', $match );
				$year = substr($date, 0, 4);	// 年
				$month= substr($date, 4, 2);	// 月
				$day = substr($date, 6, 2);	// 日
		/* 日付として扱えそうでないなら、白旗を揚げる*/		
		
				if ( strlen( (string) $year) !== 4 or !is_numeric( $year ) ) { wp_die( $year.'投稿年が取得できませんでした' );}
				if ( strlen( (string) $month ) !== 2 or !is_numeric( $month ) ) { wp_die( '投稿月が取得できませんでした' );}
				if ( strlen( (string) $day ) !== 2 or !is_numeric( $day ) ) { wp_die( '投稿日が取得できませんでした' );}
		/* リンクをmd5に置換する 
		  正規表現で拾っているのは、もともとカレンダーが持っているリンクで、カテゴリを指定したリンクだけとは限らない、
			使わないリンクは、後で、一括して削除できるようにmd5にしておく
		*/		
				
				$output= str_replace( $matches[0][ $key ], md5( $matches[0][ $key ] ), $output );
				
					$posts= $wpdb->get_results("
						SELECT ID, post_title 
						FROM $wpdb->posts 
						 INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID= $wpdb->term_relationships.object_id) 
						 INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id= $wpdb->term_taxonomy.term_taxonomy_id) 
						WHERE YEAR(post_date)= '$year' 
						 AND MONTH(post_date)= '$month' 
						 AND DAY(post_date)= '$day' 
						 AND post_type= 'post' AND post_status= 'publish' 
						 AND $wpdb->term_taxonomy.taxonomy= 'category' 
						 AND $wpdb->term_taxonomy.term_id IN ($category_ids) ORDER BY post_date DESC");
						 
				foreach($posts as $post_key=>$post){
				
				
					$link .= '<a class="post" href="'. get_permalink($post->ID) . '">' . $post->post_title . '</a>';
					
					if( in_category( array($category_ids) ) and !empty( $link ) ) {
/* リンクすべきものは、置換する */		
						$output= str_replace(md5($matches[0][ $key ]), '<td class="event" ' . $matches[1][ $key ] . '>' . $day . $link.'</td>' , $output) ;
					} 
				}
			}
		}
/* 無用なリンクは、一括して削除*/
		$output= preg_replace( '![0-9A-f]{32}!', '' , $output );
		echo $output;
		delete_transient( 'my_event_calendar_posts' );
		set_transient( 'my_event_calendar_posts',$output, 3600 );
	}
?>
<style scoped>
#wp-calendar caption,
#wp-calendar{width:auto;background:#000;}
#wp-calendar caption,
#wp-calendar a{color:#fff;}
#wp-calendar caption{
text-align:center;
width:auto;

}
#wp-calendar th{
background-color:#333;
border:1px solid #231F1D;
font-style:normal;
height:2.5em;
text-align:center;
width:85px;
}
#wp-calendar td{
color:#444;
background-color:#1D1A18;
border:1px solid #231F1D;
height:auto;
padding:3px;
text-align:center;
vertical-align:top;
font-size:36px;
font-family:Georgia, "Times New Roman", Times, serif;
font-style:italic;
}
#wp-calendar td.event{color:#888;}
#wp-calendar td a{
display:block;
font-size:12px;
font-style:normal;
margin:.5em 0;
text-align:left;
}
#wp-calendar td.pad{background-color:transparent;} // カレンダー上で日付がない箇所
#wp-calendar tfoot{display:none;} // 前後月へのリンク
</style>
</div>

[emulsion_relate_posts]