WordPressで年別ではなく年度別のアーカイブリンクの一覧を表示できないかと探していたのですが、目的に合ったものが見つからなかったので作成してみました。
実行環境
WordPress 2.7.0
PHP 4.4.4
MySql 4.0.25
ソースコード
<?php function get_archives_fiscal_year($args = '') { global $wpdb, $wp_locale; $defaults = array( 'type' => 'yearly', 'limit' => '', 'format' => 'html', 'before' => '', 'after' => '', 'show_post_count' => false, 'echo' => 1,'mcat' => false ); $r = wp_parse_args( $args, $defaults ); extract( $r, EXTR_SKIP ); if ( '' != $limit ) { $limit = absint($limit); $limit = ' LIMIT '.$limit; } // this is what will separate dates on weekly archive links $archive_week_separator = '–'; // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride $archive_date_format_over_ride = 0; // options for daily archive (only if you over-ride the general date format) $archive_day_date_format = 'Y/m/d'; // options for weekly archive (only if you over-ride the general date format) $archive_week_start_date_format = 'Y/m/d'; $archive_week_end_date_format = 'Y/m/d'; if ( !$archive_date_format_over_ride ) { $archive_day_date_format = get_option('date_format'); $archive_week_start_date_format = get_option('date_format'); $archive_week_end_date_format = get_option('date_format'); } //filters $where = apply_filters('getarchives_where', " WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND $wpdb->term_relationships.term_taxonomy_id = '$mcat' AND post_type = 'post' AND post_status = 'publish' ", $r ); $join = apply_filters('getarchives_join', "", $r); $output = ''; $query = "SELECT DISTINCT YEAR(ADDDATE(post_date, INTERVAL -3 MONTH)) AS `year`, count(ID) as posts FROM $wpdb->posts, $wpdb->term_relationships $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit"; $key = md5($query); $cache = wp_cache_get( 'wp_get_archives' , 'general'); if ( !isset( $cache[ $key ] ) ) { $arcresults = $wpdb->get_results($query); $cache[ $key ] = $arcresults; wp_cache_add( 'wp_get_archives', $cache, 'general' ); } else { $arcresults = $cache[ $key ]; } if ($arcresults) { $afterafter = $after; $preret = 0; foreach ( (array) $arcresults as $arcresult) { if( $preret == $arcresult->year ) continue; $preret = $arcresult->year; $url = get_year_link($arcresult->year)."&cat=".$mcat; $text = sprintf('%d', $arcresult->year)."年度"; if ($show_post_count) $after = ' ('.$arcresult->posts.')' . $afterafter; $output .= get_archives_link($url, $text, $format, $before, $after); } } if ( $echo ) echo $output; else return $output; } ?>
この関数はWordPressの標準関数wp_get_archivesを参考に作成しています。
45行目のSQL文の選択列を下記のようにマイナス3ヵ月することで、年度の数字を取得することができます。
DISTINCT YEAR(ADDDATE(post_date, INTERVAL -3 MONTH))
e.g)
「2010年3月31日」に投稿があった場合に、ADDDATEが実行された時点で「2009年12月31日」、この年の部分を取得することで「2009年度」という結果が得られます。
使用方法
上記のコードを使用しているテーマのテンプレートのfunctions.phpに追記してください(プラグインでも可)。
後はサイドバー等、年度別アーカイブ一覧を表示したい箇所に下記のタグを記述するだけです。
<?php get_archives_fiscal_year(); ?>
年度に特化しているため、時間のオプションは無視するようにしていますが、
その他のオプションはwp_get_archivesで使用可能なものは使えると思います。
(試してません。。。ゴメンナサイ)
年度別アーカイブ一覧から年度を選択した時のリンク先については下記のWordPressフォーラムを参照してください。
http://ja.forums.wordpress.org/topic/1192
ここではプラグインとして紹介されていますが、functions.phpに追記でも大丈夫だと思います。
※ ご使用の際は自己責任にてお願いします。
[…] 年度別アーカイブ 検索:WordPress 記事 […]
[…] 年度別アーカイブ […]