Posts WordPressの年度別表示用プラグインを作成した
Post
Cancel

WordPressの年度別表示用プラグインを作成した

年度表示させるプラグインは探したが見つからなかったので作成した。(ソースは下記)
年別ではなく、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' => &#8221;,

  
&#8216;before&#8217; => &#8221;,  
&#8216;after&#8217; => &#8216;年度&#8217;  
);

extract($r, EXTR_SKIP);

if ($limit != &#8221;) {  
$limit = absint($limit);  
$limit = &#8216; LIMIT &#8216;.$limit;  
}  
$where = apply\_filters(&#8216;getarchives\_where&#8217;, &#8220;WHERE post\_type = &#8216;post&#8217; AND post\_status = &#8216;publish'&#8221;, $r);  
$join = apply\_filters(&#8216;getarchives\_join&#8217;, &#8220;&#8221;, $r);

$query = &#8220;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&#8221;;  
$key = md5($query);  
if(!isset($cache[$key])) {  
$arcresults = $wpdb->get_results($query);  
$cache[$key] = $arcresults;  
wp\_cache\_set(&#8216;wp\_get\_archives&#8217;, $cache, &#8216;general&#8217; );  
}else{  
$arcresults = $cache[$key];  
}

$output\_key = md5(&#8220;fy\_output_key&#8221;);  
if(!isset($cache[$output_key])) {  
if($arcresults) {  
$afterafter = $after;  
foreach ((array) $arcresults as $arcresult) {  
$url = get\_year\_link($arcresult->year);  
$text = sprintf(&#8216;%d&#8217;, $arcresult->year);  
if($show\_post\_count) $after = &#8216;&nbsp;(&#8216;.$arcresult->posts.&#8217;)&#8217;.$afterafter;  
$output = $output.&#8217;

  * [&#8216;.$before.$text.$after.&#8217;]('.$url.')
&#8216;;  
}  
}  
$cache[$output_key] = $output;  
wp\_cache\_set(&#8216;wp\_get\_archives&#8217;, $cache, &#8216;general&#8217; );  
}else{  
$output = &#8220;&#8221;;  
} 

return $output;  
}

add\_filter( &#8216;get\_archives\_link&#8217;, &#8216;get\_archives_fy&#8217;);

function query\_for\_fiscal_year( $where ) {  
global $wpdb, $wp\_query, $fiscal\_year;  
if ( is_year() ) {  
$fiscal\_year = (int)$wp\_query->query_vars[&#8216;m&#8217;];  
$next\_year = $fiscal\_year + 1;  
$where = &#8221;  
AND ((YEAR($wpdb->posts.post\_date) = &#8216;$fiscal\_year&#8217; AND MONTH($wpdb->posts.post_date) BETWEEN &#8216;4&#8217; AND &#8217;12&#8217;)  
OR (YEAR($wpdb->posts.post\_date) = &#8216;$next\_year&#8217; AND MONTH($wpdb->posts.post_date) BETWEEN &#8216;1&#8217; AND &#8216;3&#8217;))  
AND $wpdb->posts.post_type = &#8216;post&#8217;  
AND ($wpdb->posts.post\_status = &#8216;publish&#8217; OR $wpdb->posts.post\_status = &#8216;private&#8217;)&#8221;;  
}

return $where;  
}

add\_action( &#8216;posts\_where&#8217;, &#8216;query\_for\_fiscal_year&#8217; );

?>

PHPによるWordPressカスタマイズブックという本が出ていてプラグインの自作方法が載っていた。Wordpressサイトのドキュメントより少し詳しいという感じだった。良さげな本なので是非買いたい。