0

Cant seem to return to the index page with the Previous link using paginate_links(). Previous link is linking to paged 2.

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
 $_args = array(
   'orderby'       =>  'post_date',
   'order'         =>  'DSC',
   'paged'         => $paged,
   'post_type' => 'post',
   'posts_per_page' => 2, // I have more but just show to pagination
   'post_status'    => 'publish'
  );

  $_posts = new WP_Query( $_args );


$arr = paginate_links(array(
  'base'               => '%_%',
    'format'             => '?paged=%#%',
    'total'              => $_posts->max_num_pages,
    'current'            => max( 1, get_query_var('paged') ),
    'show_all'           => false,
    'end_size'           => 1,
    'mid_size'           => 2,
    'prev_next'          => true,
    'prev_text'          => __('« Previous'),
    'next_text'          => __('Next »'),
    'type'               => 'array',
    'add_args'           => false,
    'add_fragment'       => '',
    'before_page_number' => '',
    'after_page_number'  => ''
));

foreach( (array) $arr as $link ) {
  var_dump( $link );
}

On ?paged=2, the Previous link is still pointing to ?paged=2 when it should return to the index page, the page where I should see all posts or return to the previous page. What is going on, please?

1

You are using the code from the example in the Codex, but this is for the default "plain" permalinks for the default blog homepage. My guess is that you are doing this for an archive rather than the homepage.

Search & Archive pages

By default, paginate_links assumes the pagination is for the blog on the homepage. In all other cases, you need to change the code as follows (Ref: Codex: paginate_links):

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',

    // rest of your arguments here

) );

What this does:

We need to change paginate_links to make it use the link for the archive page instead of the homepage. We do this by changing the base argument to include the url. We also need to change the format to use the correct parameter.

  1. Create a variable with a huge number that's unlikely to ever be a valid page number, e.g.
    $big = 999999999;

  2. set up your base argument

For base, you need to tell paginate_links to use the url. We do this by using get_pagenum_link to get the url for a page that will never exist, and then we replace the page number in the url with the actual page number

'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) )
  1. set up your format argument

format defaults to use the page parameter (for the static homepage) so we need to change it to use paged instead:

    'format' => '?paged=%#%',


NOTE: Pretty Permalinks

If you are displaying the pagination on the homepage and using pretty permalinks, but are still having issues, try the following.

According to the Codex, if you are using "pretty" permalinks, you should change the format as follows:

'format'             => '/page/%#%',

format
(string) (optional) Used for Pagination structure. The default value is ?page=%#%, If using pretty permalinks this would be /page/%#%, where the %#% is replaced by the page number.

This is good practice, but its not required because Wordpress rewrite rules will automatically translate the ?paged parameter

0

I think you are passing to the query what you have in the url when clicking on the previous link.

Have you tried to echo the links with base param instead of foreaching?

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $_posts->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.