wp_get_archives() を使用して日付アーカイブを出したときに
●●●●年●月●日 となるのを
●●●●/●●/●● と月・日の数字を二桁にしつつ区切り文字を / に変更する。
functions.phpに追記。
/* アーカイブリンクの日付書式変更
------------------------------------------------------------ */
function my_archives_link($link_html){
$link_html = preg_replace('/(年)([1-9]月)/', '${1}0$2', $link_html);
$link_html = preg_replace('/(月)([1-9]日)/', '${1}0$2', $link_html);
$link_html = str_replace('年','/',$link_html);
$link_html = str_replace('月','/',$link_html);
$link_html = str_replace('日','',$link_html);
return $link_html;
}
add_filter('get_archives_link', 'my_archives_link');
●●●●年●月 を ●●●●/●● に変更する場合は以下
/* アーカイブリンクの日付書式変更
------------------------------------------------------------ */
function my_archives_link($link_html){
$link_html = preg_replace('/(年)([1-9]月)/', '${1}0$2', $link_html);
$link_html = str_replace('年','/',$link_html);
$link_html = str_replace('月','',$link_html);
return $link_html;
}
add_filter('get_archives_link', 'my_archives_link');
おまけ
日付アーカイブで wp_title() の出力結果に年と日が付かないのを修正したり書式変更したりするサンプル
年月日表示の際は//のコメントアウトを取り払って次の行を消す。
/* wp_title()の日付アーカイブのタイトルを変更
------------------------------------------------------------ */
function adjust_date_title( $title, $sep, $seplocation ) {
$m = get_query_var( 'm' );
$year = get_query_var( 'year' );
$monthnum = get_query_var( 'monthnum' );
$day = get_query_var( 'day' );
$date_title = '';
/* mパラメータがある場合 (パーマリンク設定がデフォルトの場合の日付アーカイブ) */
if ( is_archive() && ! empty( $m ) ) {
$my_year = substr( $m, 0, 4 );
$my_month = substr( $m, 4, 2 );
$my_day = substr( $m, 6, 2 );
//$date_title = $my_year . '年' . ( $my_month ? $my_month . '月' : '' ) . ( $my_day ? $my_day . '日' : '' );
$date_title = $my_year . '/' . ( $my_month ? $my_month . '/' : '' ) . ( $my_day ? $my_day : '' );
}
/* yearパラメータがある場合 (パーマリンク設定がデフォルト以外の日付アーカイブ) */
if ( is_archive() && ! empty( $year ) ) {
//$date_title = $year . '年';
$date_title = $year;
if ( ! empty( $monthnum ) ) {
//$date_title .= zeroise( $monthnum, 2 ) . '月';
$date_title .= '/' . zeroise( $monthnum, 2 );
}
if ( ! empty( $day ) ) {
//$date_title .= zeroise( $day, 2 ) . '日';
$date_title .= '/' . zeroise( $day, 2 );
}
}
/* 日付調整を行ったタイトルがあれば区切り文字を追加(左か右) */
if ( '' != $date_title ) {
if ( 'right' == $seplocation ) {
$title = $date_title . " $sep ";
} else {
$title = " $sep " . $date_title;
}
}
return $title;
}
add_filter( 'wp_title', 'adjust_date_title', 10, 3 );