サイドバーの Archives で日付表記を変更する

サイドバーの Archives で日付表記を変更する

2006/12/08 9:30am

WordPress のサイドバーに表示されている「Archives」だが、日付の表記が「November 2006」のようになっている。私の推測では November が何月なのか、すぐにわかる人はすくないはずだ。できれば、ここはなんとかしたい。

管理画面の「Options」にあった「Date and Time」の設定を変更してみた。しかし、どこにも反映されている気配がないので(わざわざ PHP のドキュメントを読んだっていうのに…)、ソースコードを調べることにする。

テンプレートを眺めてみると、wp_get_archives という PHP の関数を呼び出している。で、こいつは引数の解析だけやって、実際の処理は get_archives 関数が行う。

ファイルは wp-includes/template-functions-general.php の 300 行目あたり。

function get_archives($type='', $limit='', $format='html', $before = '', $after = '', $show_post_count = false) {
  ...
  if ( 'monthly' == $type ) {
    ...
        if ( $show_post_count ) {
          **$text = sprintf('%s %d', $month[zeroise($arcresult->month,2)], $arcresult->year);**
          $after = ' ('.$arcresult->posts.')' . $afterafter;
        } else {
          **$text = sprintf('%s %d', $month[zeroise($arcresult->month,2)], $arcresult->year);**
        }
        echo get_archives_link($url, $text, $format, $before, $after);
      }
    }
  } elseif ( 'daily' == $type ) {
  ...

どうやら月別表示のフォーマットは sprintf で決め打ちらしいので、アドホックなコードにはアドホックな修正で対応する。PHP でプログラミングをしたことはないが、まあ適当に書いたら動くだろう、という魂胆。

function get_archives($type='', $limit='', $format='html', $before = '', $after = '', $show_post_count = false) {
  ...
  if ( 'monthly' == $type ) {
      $url = get_month_link($arcresult->year, $arcresult->month);
      **$text = sprintf('%04d/%02d', $arcresult->year, $arcresult->month);**
      if ( $show_post_count ) {
        $after = ' ('.$arcresult->posts.')' . $afterafter;
      }

これで「November 2006」が「2006/11」と表示されるようになった。