This is really weird. In attempting to solve this problem i ended up with an almost perfect use of paginate_links() instead of a custom pagination function:
$myquery = new WP_Query($args);
$paged = get_query_var('page');
($paged == 0 ? $paged = 1 : $paged = $paged);
$pagination = paginate_links(array(
'total' => $myquery->max_num_pages,
'current' => $paged,
'show_all' => true,
'type' => 'list',
));
This seemed to work fine. Until i found out that no matter what i do, whenever i'm after page 1, the function prints an empty href=""
on my first links.
So i get these markups:
<!-- base page - example.com/parent-page/child-page/ -->
<!-- same for page 1 - example.com/parent-page/child-page/?page=1 -->
<ul class="page-numbers">
<li><span class="page-numbers current">1</span></li>
<li><a class="page-numbers" href="?page=2">2</li>
<li><a class="page-numbers" href="?page=3">3</li>
<li><a class="page-numbers" href="?page=4">4</li>
<li><a class="next page-numbers" href="?page=2">Next</li>
</ul>
<!-- page 2 - example.com/parent-page/child-page/?page=2 -->
<ul class="page-numbers">
<li><a class="prev page-numbers" href="">Previous</a></li> <!-- empty href="" -->
<li><a class="page-numbers" href="">1</li><!-- empty href="" -->
<li><span class="page-numbers current">2</span></li>
<li><a class="page-numbers" href="?page=3">3</li>
<li><a class="page-numbers" href="?page=4">4</li>
<li><a class="next page-numbers" href="?page=2">Next</li>
</ul>
<!-- page 3 and onwards - example.com/parent-page/child-page/?page=3 -->
<ul class="page-numbers">
<li><a class="prev page-numbers" href="?page=2">Previous</a></li> <!-- correct -->
<li><a class="page-numbers" href="">1</li><!-- empty href="" -->
<li><a class="page-numbers" href="?page=2">2</li>
<li><span class="page-numbers current">3</span></li>
<li><a class="page-numbers" href="?page=4">4</li>
<li><a class="next page-numbers" href="?page=2"></li>
</ul>
Digging into paginate_links() on wp-includes/general-template.php (circa line 1954) i found that for some reason it explicitly passes empty link arguments to the Previous link when on page 2:
if ( $prev_next && $current && 1 < $current ) :
$link = str_replace('%_%', 2 == $current ? '' : $format, $base);
and to the first link (line 1968)
if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
$link = str_replace('%_%', 1 == $n ? '' : $format, $base);
any thoughts on why that would be, and if it's possible to avoid it w/o creating yet another pagination function?
'/blog/?page=2'
, but the "href" generated by Wordpress would just be'?page=2'
, which all works fine. However, the page querystring is left off for page 1 (instead of doing ?page=1). Subsequently, the "href" ends up as "". The given answers forcebase
to include the current page in the href, so even without a querystring it still points to a page. – Khan Jul 14 '14 at 17:36