年度表示させるプラグインは探したが見つからなかったので作成した。(ソースは下記)
年別ではなく、4月1日から翌年3月31日までを括る「会計年度」別表示。
年度表示にするには下記のfyplugin.phpをプラグインフォルダに置いて有効にしたあとウィジェットをドラッグ&ドロップする。
– 経緯 –
年度別アーカイブを表示するソースはWordPressのコミュニティに掲載されていたので
そのまま使わせてもらっている。ありがたい。
年度別アーカイブへのリンクをサイドバーに表示させるソースは決定的なものが見つからなかった。
その中で標準のwp_get_archives関数を書き換えて作成している人がいたのでそれに倣ってwp_get_archives関数を書き換えて作成。
ちなみにこのブログではこのプラグインを使っていません。
環境
WordPress3.0.4
WordPress3.1
WordPress3.3
fyplugin.php
<?php /* Plugin Name: fy plugin Plugin URI: Description: 年度別表示用プラグイン Author: mn-memo Version: 1.4 */ function get_archives_fy($link_html) { global $wpdb; $cache = wp_cache_get('wp_get_archives', 'general'); $r = array( 'limit' => '', 'before' => '', 'after' => '年度' ); extract($r, EXTR_SKIP); if ($limit != '') { $limit = absint($limit); $limit = ' LIMIT '.$limit; } $where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r); $join = apply_filters('getarchives_join', "", $r); $query = "SELECT YEAR(ADDDATE(post_date, INTERVAL -3 MONTH)) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(ADDDATE(post_date, INTERVAL -3 MONTH)) ORDER BY post_date DESC $limit"; $key = md5($query); if(!isset($cache[$key])) { $arcresults = $wpdb->get_results($query); $cache[$key] = $arcresults; wp_cache_set('wp_get_archives', $cache, 'general' ); }else{ $arcresults = $cache[$key]; } $output_key = md5("fy_output_key"); if(!isset($cache[$output_key])) { if($arcresults) { $afterafter = $after; foreach ((array) $arcresults as $arcresult) { $url = get_year_link($arcresult->year); $text = sprintf('%d', $arcresult->year); if($show_post_count) $after = ' ('.$arcresult->posts.')'.$afterafter; $output = $output.'<li><a href="'.$url.'">'.$before.$text.$after.'</a></li>'; } } $cache[$output_key] = $output; wp_cache_set('wp_get_archives', $cache, 'general' ); }else{ $output = ""; } return $output; } add_filter( 'get_archives_link', 'get_archives_fy'); function query_for_fiscal_year( $where ) { global $wpdb, $wp_query, $fiscal_year; if ( is_year() ) { $fiscal_year = (int)$wp_query->query_vars['m']; $next_year = $fiscal_year + 1; $where = " AND ((YEAR($wpdb->posts.post_date) = '$fiscal_year' AND MONTH($wpdb->posts.post_date) BETWEEN '4' AND '12') OR (YEAR($wpdb->posts.post_date) = '$next_year' AND MONTH($wpdb->posts.post_date) BETWEEN '1' AND '3')) AND $wpdb->posts.post_type = 'post' AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private')"; } return $where; } add_action( 'posts_where', 'query_for_fiscal_year' ); ?>
PHPによるWordPressカスタマイズブックという本が出ていてプラグインの自作方法が載っていた。Wordpressサイトのドキュメントより少し詳しいという感じだった。良さげな本なので是非買いたい。