WordPressのアーカイブページに月毎のページ送りを設置する方法。
下記ブログを参照(まんま流用)して、ページ送りを作成する。
参照元:前の月と次の月のリンク
<?php
$link_next_month = '';
$link_prev_month = '';
global $wpdb;
$thisyear = intval( get_the_time( 'Y' ) );
$thismonth = intval( get_the_time( 'm' ) );
$previous = $wpdb->get_row(
"SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date < '$thisyear-$thismonth-01'
AND post_type = 'post'
AND post_status = 'publish'
ORDER BY post_date DESC
LIMIT 1");
$next = $wpdb->get_row(
"SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date > '$thisyear-$thismonth-01'
AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' )
AND post_type = 'post'
AND post_status = 'publish'
ORDER BY post_date ASC
LIMIT 1");
if( $previous ) $link_prev_month = get_month_link( $previous->year, $previous->month );
if( $next ) $link_next_month = get_month_link( $next->year, $next->month );
?>
これで表示月の前月($link_prev_month)と次月($link_next_month)のリンク先URLを出得することができる。
あとは、こいつらを必要な場所に記述してあげれば完了。
アーカイブページのタイトル「月毎のアーカイブ:0000年00月」の前後にそれぞれ前月と次月のリンク表示をつけてやる。
archive.phpの、
elseif ( is_month() ) :
printf( __( 'Monthly Archives: %s', 'twentyfourteen' ), get_the_date( _x( 'F Y', 'monthly archives date format', 'twentyfourteen' ) ) );
の部分から、「月毎のアーカイブ:」の表示を取り除き、『≪前月|該当月|次月≫」のような左に前月、右に次月の表示を、あれば(最初の月は前月が、最新の月は次月が無い)記述する。
elseif ( is_month() ) :
if( $previous ): echo '<a href="'.$link_prev_month.'">«'.$previous->year.'年'.$previous->month.'月</a>|'; endif;
printf( get_the_date( _x( 'F Y', 'monthly archives date format', 'twentyfourteen' ) ) );
if( $next ): echo'|<a href="'.$link_next_month.'">'.$next->year.'年'.$next->month.'月»</a>'; endif;
あとはCSSで整形して出来上がり
ちなみに「前年」「次年」と年毎の場合は、同じ要領が下記ブログにありました。
参照:WordPressのアーカイブページで「前の年」「次の年」のページ送りをつける方法。
下記ブログを参照(まんま流用)して、ページ送りを作成する。
参照元:前の月と次の月のリンク
<?php
$link_next_month = '';
$link_prev_month = '';
global $wpdb;
$thisyear = intval( get_the_time( 'Y' ) );
$thismonth = intval( get_the_time( 'm' ) );
$previous = $wpdb->get_row(
"SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date < '$thisyear-$thismonth-01'
AND post_type = 'post'
AND post_status = 'publish'
ORDER BY post_date DESC
LIMIT 1");
$next = $wpdb->get_row(
"SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date > '$thisyear-$thismonth-01'
AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' )
AND post_type = 'post'
AND post_status = 'publish'
ORDER BY post_date ASC
LIMIT 1");
if( $previous ) $link_prev_month = get_month_link( $previous->year, $previous->month );
if( $next ) $link_next_month = get_month_link( $next->year, $next->month );
?>
これで表示月の前月($link_prev_month)と次月($link_next_month)のリンク先URLを出得することができる。
あとは、こいつらを必要な場所に記述してあげれば完了。
アーカイブページのタイトル「月毎のアーカイブ:0000年00月」の前後にそれぞれ前月と次月のリンク表示をつけてやる。
archive.phpの、
elseif ( is_month() ) :
printf( __( 'Monthly Archives: %s', 'twentyfourteen' ), get_the_date( _x( 'F Y', 'monthly archives date format', 'twentyfourteen' ) ) );
の部分から、「月毎のアーカイブ:」の表示を取り除き、『≪前月|該当月|次月≫」のような左に前月、右に次月の表示を、あれば(最初の月は前月が、最新の月は次月が無い)記述する。
elseif ( is_month() ) :
if( $previous ): echo '<a href="'.$link_prev_month.'">«'.$previous->year.'年'.$previous->month.'月</a>|'; endif;
printf( get_the_date( _x( 'F Y', 'monthly archives date format', 'twentyfourteen' ) ) );
if( $next ): echo'|<a href="'.$link_next_month.'">'.$next->year.'年'.$next->month.'月»</a>'; endif;
あとはCSSで整形して出来上がり
ちなみに「前年」「次年」と年毎の場合は、同じ要領が下記ブログにありました。
参照:WordPressのアーカイブページで「前の年」「次の年」のページ送りをつける方法。