年度表示させるプラグインは探したが見つからなかったので作成した。(ソースは下記)
年別ではなく、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.’
* [‘.$before.$text.$after.’]('.$url.')
‘;
}
}
$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サイトのドキュメントより少し詳しいという感じだった。良さげな本なので是非買いたい。