wp_get_archives 英語置換・2017年1月 (n) -> January 2017 (n)
「2017年1月」を「01 2017」にテキスト置換&左右入れ替え。
1月のままだと11月も置換対象に含まれてしまうため、差異化のため先頭に0を追加。
1 2 3 4 5 6 7 | function my_gettext($translate, $original_text, $domain) { if ($original_text == '%1$s %2$d') { $translate = '%1$02d月 %2$s'; } return $translate; } add_filter('gettext', 'my_gettext', 20, 3); |
wp_get_archives は、
get_archives_link() を呼び出して、一つ一つのアーカイブリンクの文字列を組み立てます。
ということなので、get_archives_linkフィルターフックを使い、str_replaceでnn月を英単語にテキスト置換。
コード冗長だが、格納配列したらエラーになったのでそのまま。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function my_archives_link($my_text) { $my_text = str_replace('01月', 'January', $my_text); $my_text = str_replace('02月', 'February', $my_text); $my_text = str_replace('03月', 'March', $my_text); $my_text = str_replace('04月', 'April', $my_text); $my_text = str_replace('05月', 'May', $my_text); $my_text = str_replace('06月', 'June', $my_text); $my_text = str_replace('07月', 'July', $my_text); $my_text = str_replace('08月', 'August', $my_text); $my_text = str_replace('09月', 'September', $my_text); $my_text = str_replace('10月', 'October', $my_text); $my_text = str_replace('11月', 'November', $my_text); $my_text = str_replace('12月', 'December', $my_text); $my_text = preg_replace('/<\/a> (\([0-9]*\))/', ' $1</a>', $my_text); // (n)をリンク内側に return $my_text; } add_filter('get_archives_link', 'my_archives_link'); |
月別アーカイブページの出力テキストを、switch文で英語化。
break 無しでも使える。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function en_month($month) { switch ($month) { case '1月' : return 'January'; case '2月' : return 'February'; case '3月' : return 'March'; case '4月' : return 'April'; case '5月' : return 'May'; case '6月' : return 'June'; case '7月' : return 'July'; case '8月' : return 'August'; case '9月' : return 'September'; case '10月' : return 'October'; case '11月' : return 'November'; case '12月' : return 'December'; } } |
サイドバーなど任意の場所で、wp_get_archivesを出力。
リンク付きの 2017年1月 (n) が、January 2017 (n) に変化。
1 2 3 4 5 6 7 8 9 10 11 12 | <ul> <?php $my_archives = wp_get_archives( array( 'limit' => '12', 'show_post_count' => 1, 'echo' => 0 ) ); echo $my_archives; ?> </ul> |
月別アーカイブページで年月テキストを出力。
2017年1月 が、January 2017 に変化。
1 2 3 4 5 6 7 | <div> <?php $month = get_the_date('M'); $year = get_the_date(' Y'); echo en_month($month), $year; ?> </div> |
WordPressで str_replace を積極的に使うべきではないが、これ以外の方法を思いつかず。
コメントを投稿する