ページネーション | WordPress
ページネーション
アーカイブ、カテゴリーのページネーション。
URLとしてhttps://example.com/page/2の「2」は、パブリッククエリpageではなくpagedの値になる(WordPressの決まり)。
以下は勘違いしやすいので注意。
- paged:ページネーション用のパブリッククエリ
- page:一つの投稿を複数ページに分割(投稿本文で
<!--nextpage-->を入力した場合)したときのページ番号
参考: https://wordpress.stackexchange.com/questions/180784/what-is-the-difference-between-paged-and-page
function get_the_posts_pagination_advance() {
/*
* get_the_posts_pagination
* @see https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/get_the_posts_pagination
*
* get_the_posts_pagination([
* 'prev_text' => __( '« Previous', 'first' ),
* 'next_text' => __( 'Next »', 'first' ),
* ]);
*
* <nav class="navigation pagination" role="navigation" aria-label="投稿">
* <h2 class="screen-reader-text">投稿ナビゲーション</h2>
* <div class="nav-links">
* <span aria-current="page" class="page-numbers current">1</span>
* <a class="page-numbers" href="http://localhost:8888/page/2">2</a>
* <span class="page-numbers dots">…</span>
* <a class="page-numbers" href="http://localhost:8888/page/5">5</a>
* <a class="next page-numbers" href="http://localhost:8888/page/2">Next »</a></div>
* </nav>
*/
$defaultPageNation = get_the_posts_pagination( array(
'prev_text' => __( '« Previous', 'first' ),
'next_text' => __( 'Next »', 'first' ),
) );
// Get current page. If page is first, then set $paged = 1
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// Total pages.
$maxPage = $GLOBALS['wp_query']->max_num_pages;
// Add link to first page if current is not first page
$firstPageLink = ( $paged !== 1 ) ? sprintf( '<a href="%s">最初へ</a>', get_pagenum_link( 1 ) ) : '';
// Add link to last page if current is not last page(max page)
$lastPageLink = ( $paged !== $maxPage ) ? sprintf( '<a href="%s">最後へ</a>', get_pagenum_link( $maxPage ) ) : '';
$dom = new DOMDocument();
@$dom->loadHTML( $defaultPageNation );
$xpath = new DOMXPath( $dom );
$listNodes = $xpath->query( '//div[@class="nav-links"]' );
$listNode = $listNodes->item( 0 );
echo $dom->saveHTML( $listNode );
}
※ 元のコードは$defaultPageNation = $pageNation = get_the_posts_pagination(...)と代入していたが、$pageNation変数はどこでも使われていなかったため、不要な代入を削除して整理した。