I’m using the following code to generate some pagination:
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=5'.'&paged='.$paged);
$big = 999999999;
echo '<div class="pagination">';
echo paginate_links(array( 'base' => '%_%',
'format' => str_replace($big, '%#%', esc_url(get_pagenum_link( $big ))),
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'end_size' =>4,
'type' => 'list'));
echo '</div>';
Its generating my links correctly on the first page, but if I go to any other page everything is still correct except the link for page 1 is always the url of whatever page I am on. Seems like I’m missing somthing simple, anyone know a fix?
Short answer:
Try
Long answer:
I took a look at the
paginate_links()
source code (v3.5.1) and there is this line (#)that is giving you the empty first page link.
With your setup you have
$base = "%_%"
and$format = "http://example.com/page/%#%/"
so this becomes:where we have two cases:
and after the replacement:
Here is an example of the output from
paginate_links()
:If you use instead (#):
then you get:
Since no replacement will take place
in both cases (n=1 and n>1) and you have a non empty first page link with the output of
paginate_links()
:To have a non empty first page link it looks like
$format
can be any string as long as$base
doesn’t include the string"%_%"
, i.e. these should work fine:If you don’t use permalinks, then the example in (#) will also give you non empty first page link since
with replacements.
Thanks birgire for help. I’m sharing the result of my paginate_links() which are working with custom taxonomy pages exactly as I need.
Hope this will help someone to fix it as quick as possible.
So here it is:
I stumbled on a same problem and found out that I can replace:
with:
So just renamed ‘paged’ >’page’ in query var and it works.
Bobz solution worked for me: