アーカイブ、カテゴリーのページネーション。
ここでURLとしてはhttps://example.com/page/2
の2はパブリッククエリpage
ではなくpaged
の値になる(WordPressの決まり)。
以下は勘違いしやすいので注意。
<!--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 = $pageNation =get_the_posts_pagination([
'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);
}