LoginSignup
4
1

More than 1 year has passed since last update.

アーカイブ一覧(wp_get_archives)の月表記を英語表記にする

Last updated at Posted at 2018-03-15

テンプレートファイル以外はなるべく修正したくないので、functions.php内で完結させました。

何もしなければ

2018年3月

と、なる表記を

2018, March

に変更させます。

functions.php
/*英語表記に変換関数*/
function encode_date( $m ) {
	if(strpos($m,'11月') !== false){return 'November';}
	elseif(strpos($m,'12月') !== false){return 'December';}
	elseif(strpos($m,'1月') !== false){return 'January';}
	elseif(strpos($m,'2月') !== false){return 'February';}
	elseif(strpos($m,'3月') !== false){return 'March';}
	elseif(strpos($m,'4月') !== false){return 'April';}
	elseif(strpos($m,'5月') !== false){return 'May';}
	elseif(strpos($m,'6月') !== false){return 'June';}
	elseif(strpos($m,'7月') !== false){return 'July';}
	elseif(strpos($m,'8月') !== false){return 'August';}
	elseif(strpos($m,'9月') !== false){return 'September';}
	elseif(strpos($m,'10月') !== false){return 'October';}
}

function my_archives_link($html){
	$posY = mb_strpos($html, '年')+1;
	$posM = mb_strpos($html, '月');
	$m = mb_substr($html, $posY, $posM-$posY);//n月を抜き出して変数に入れる
	$m = trim($m);
	$mEn = encode_date((string) $m);
	$html = str_replace($m,$mEn,$html);
	$html = str_replace('年',', ',$html);

  return $html;
}

add_filter('get_archives_link', 'my_archives_link');

サイドバーなどテンプレートに、以下を記入します。

<?php wp_get_archives(); ?>

参考サイト

http://design-studio-f.com/blog/wp-get-archives-list-customized/
https://www.nxworld.net/wordpress/wp-month-change.html
http://kudox.jp/php/string-function

2022年3月修正

すごい今更ですけれども、それぞれの月が抽出できていなかったのを修正しました。
これで記事件数も表示できる!

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1