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?
Have you tried specifying the
base
andformat
arguments forpaginate_links()
? It assumes the default values of:Your
base
should be something like/parent-page/child-page/%_%
; then the first page link will be to/parent-page/child-page/
, and subsequent links will follow the format/parent-page/child-page/?page=3
(example for page 3).In the
base
, the%_%
is replaced by theformat
argument.In the
format
, the%#%
is replaced by the page number.http://codex.wordpress.org/Function_Reference/paginate_links
For anyone else interested, here is the code that worked for me:
Hideously out of date, but I’ve been having trouble with this and I got the following to work –