Frontpage pagination issue with paginate_links() function

I’m using following snippet to output pagination:

// get the current page     
if ( get_query_var('paged') ) {
    $current_page = get_query_var('paged');
} else if ( get_query_var('page') ) {
    $current_page = get_query_var('page');
} else {
    $current_page = 1;
}

// structure of “format” depends on whether we’re using pretty permalinks
$permalink_structure = get_option('permalink_structure');
$format = empty( $permalink_structure ) ? '&page=%#%' : 'page/%#%/';

echo paginate_links(array(
      'base' => get_pagenum_link(1) . '%_%',
      'format' => $format,
      'current' => $current_page,
      'total' => $total,
      'mid_size' => 4,
      'type' => 'list'
));

When I set permalink type to http://wordpress/?p=123 and use static page for frontpage with pagination, I recieve wrong URLs like:

Read More

http://wordpress/&page=2

instead of:

http://wordpress/?page=2

If you navigate to http://wordpress/?page=2, links in pagination will be also wrong, because current URL with queries goes as a base, for example:

http://wordpress/?page=2&page=3

If I use any other permalink type with rewriting, everything works fine.

I’m looking any solution for this snippet and frontpage pagination.

Thanks in advance for any help.

UPDATE:
My main problem was a mistype at &page=%#% but actually i need &paged=%#% and i add condition with is_front_page() function to switch & to ? for frontpage query.

Related posts

Leave a Reply

1 comment

  1. I think that’s because the code is doing strictly what you’re telling it to do, here:

    $format = empty( $permalink_structure ) ? '&page=%#%' : 'page/%#%/';
    

    Note: '&page=%#%'.

    Try building your permalink structure using add_query_arg(), which handles proper appending of & vs ?.