月次アーカイブリストの prev next month ナビゲーション

WordPress 3.4までの月次アーカイブリストには、投稿ページのように、次の投稿、前の投稿などのリンクを付けるための関数は、準備されていません。

月次アーカイブを開くと、個別記事を選択するか、戻るという事になり、月次アーカイブというレベルでの、画面遷移はできません。

http://ja.forums.wordpress.org/topic/11300?replies=4(何かよい方法がないかと、質問がありました)

コードは、その時書いたものに少し手を入れたものです。

theme/functions.php

if( ! function_exists( 'raindrops_monthly_archive_prev_next_navigation' ) ){
	function raindrops_monthly_archive_prev_next_navigation(){
		global $wpdb, $wp_query;
		
		$post_date	= '';
		$thisyear	= '';
		$thismonth	= '';
		$filter_name= __FUNCTION__;
		$class_name = str_replace( '_', '-', $filter_name );	
	
		if( is_month() ){
			if ( have_posts() ) {
				$post_date = $wp_query->posts[0]->post_date;
				$thisyear = mysql2date('Y', $post_date );
				$thismonth = mysql2date('m', $post_date );
			}else{
				return;
			}
			
			$unixmonth 	= mktime(0, 0 , 0, $thismonth, 1, $thisyear);
			$last_day 	= date('t', $unixmonth);
			
			$previous 	= $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year	FROM $wpdb->posts
				WHERE post_date < '$thisyear-$thismonth-01'
				AND post_type= 'post' AND post_status= 'publish'
					ORDER BY post_date DESC
					LIMIT 1");
			$next 		= $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year FROM $wpdb->posts
				WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
				AND post_type= 'post' AND post_status= 'publish'
					ORDER BY post_date ASC
					LIMIT 1");
					
			$html 		= '<a href="%1$s" class="%3$s">%2$s</a>';
					
			if ( $previous ) {
				$calendar_output= sprintf( $html,
											get_month_link($previous->year,
											$previous->month) ,
											sprintf(__('Previous Month(%sth)','Raindrops'),
											$previous->month),
											'alignleft' 
										 );
			}
			$calendar_output 	 .= "\t" ;
			if ( $next ) {
				$calendar_output .= sprintf( $html, 
											get_month_link($next->year, 
											$next->month),
											sprintf(__('Next Month(%sth)','Raindrops'),
											$next->month),
											'alignright'
											);
			}
			
			$html				= '<div class="%1$s">%2$s</div>';
			
				$calendar_output= sprintf( $html,
											$class_name,
											$calendar_output
										);
			
			echo apply_filters( $filter_name, $calendar_output );
		}
	}
}

template file ( ex:archive.php,date.php )

<?php
if( function_exists( 'raindrops_monthly_archive_prev_next_navigation' ) ){
	raindrops_monthly_archive_prev_next_navigation();
}
?>

他の方法

get_calendar() の返り値を盗んで、リンクだけを抜き出す。
少し遊んでいます。

<?php
if( is_month() ){	
	preg_match('!<tfoot>(.*)<\/tfoot>!siu',get_calendar(true, false), $regs );
	
	if( ! empty( $regs ) ){
		$response= strip_tags( $regs[1], '<a>');
		
		echo apply_filters( 'my_codenap', $response );
		
	}elseif( WP_DEBUG ){
		echo 'HTML of the calendar maybe changed. ';	
	}
}
	
?>

テンプレートにapply_filtersを記述したので、
functions.phpから、

add_filter( 'my_codenap', 'my_filter' );

function my_filter($contents){

return '教えない';
}

といったコントロールが利くようになります。

[emulsion_relate_posts]