4

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?

1
  • On a URL like /blog/ the link to page 2 would be '/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 force base 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
3

Have you tried specifying the base and format arguments for paginate_links()? It assumes the default values of:

    'base' => '%_%',  
    'format' => '?page=%#%',  

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 the format argument.
In the format, the %#% is replaced by the page number.

http://codex.wordpress.org/Function_Reference/paginate_links

1
  • you just made my day. :) I had tried using other values on baseand format but never did it occur to me that my current URL would be useful there. I owe you a beer. – moraleida May 17 '12 at 17:22
7

For anyone else interested, here is the code that worked for me:

 echo paginate_links( array(
    'current' => max(1, get_query_var('paged')),
    'total' => $wp_query->max_num_pages,
    'base' => get_pagenum_link(1) . '%_%',  
        'format' => 'page/%#%',
) );
2

Hideously out of date, but I've been having trouble with this and I got the following to work -

echo paginate_links(array(
    'base'      => remove_query_arg('page', get_pagenum_link(1)).'%_%',
    'current'   => max(1, get_query_var('page')),
    'end_size'  => 2,
    'total'     => $this->items->max_num_pages,
));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.