カスタム投稿タイプの年別アーカイブ表示

function add_custom_rewrite_rules() {
	$rules = array(
		'top' => array(
			'^event/([0-9]{4})/page/([0-9]+)/?' => 'index.php?post_type=event&year=$matches[1]&paged=$matches[2]',
			'^event/([0-9]{4})/?' => 'index.php?post_type=event&year=$matches[1]',
		),
		'bottom' => array(
		)
	);
	foreach ( $rules as $position => $position_rules ) {
		foreach ( $position_rules as $rule => $rewrite ) {
			add_rewrite_rule($rule, $rewrite, $position );
		}
	}
}
add_action( 'init', 'add_custom_rewrite_rules' );

function get_post_type_archives_where( $where, $r ) {
	global $my_archives_post_type;
	if ( isset( $r['post_type'] ) ) {
		$my_archives_post_type = $r['post_type'];
		$where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
	} else {
		$my_archives_post_type = false;
	}
	return $where;
}
add_filter( 'getarchives_where', 'get_post_type_archives_where', 10, 2 );


function get_post_type_archives_link( $link_html ) {
	global $my_archives_post_type;
	if ( $my_archives_post_type ) {
		$link_html = preg_replace( "#(/[0-9]{4})#", '/' . $my_archives_post_type . '$1', $link_html);
		$link_html = preg_replace( "#>([0-9]{4})</a>#", '>$1年</a>', $link_html);
	}
	return $link_html;
}
add_filter( 'get_archives_link', 'get_post_type_archives_link' );

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です