Pagination with WordPress default gallery?

Here is the part of code handling pagination in functions.php:

// Pagination Setup
$current = (get_query_var('paged')) ? get_query_var( 'paged' ) : 1;
$per_page = 3;
$offset = ($current-1) * $per_page;
$big = 999999999;
$total = sizeof($attachments);
$total_pages = round($total/$per_page);
if( $total_pages  ( $total/$per_page ) ){
    $total_pages = $total_pages+1;
}

// Pagination output
$output .= paginate_links( array(
    'base' = str_replace($big,'%#%',esc_url(get_pagenum_link($big))),
    'format' = '?paged=%#%',
    'current' = $current,
    'total' = $total_pages,
    'prev_text'    = __('laquo;'),
    'next_text'    = __('raquo;')
) );

Here is the page:

https://www.motorizm.net/bugatti-type-41-royale/

The issue:

The code successfully divides images into groups by three, creates pages for these groups, creates pagination numbers and creates links to the pages. But when I click on any pagination link, I do not go anywhere.

Although when the page ( https://www.motorizm.net/bugatti-type-41-royale/ ) has Status: Draft, pagination links do work as they are supposed to.

Any ideas?

Topic gallery pagination Wordpress

Category Web


Ended up like this. Made this changes and it worked:

changed:

'base' => str_replace($big,'%#%',esc_url(get_pagenum_link($big))),

into:

'base' => get_permalink( $post->post_parent ) . '%_%',

And changed:

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

into:

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

Then added a filter with rewrite tag and rule:

add_filter('init', 'post_gallery_add_rewrite_tag_rule_2022');
function post_gallery_add_rewrite_tag_rule_2022() {
    add_rewrite_tag('%current%','([^&]+)');
    add_rewrite_rule('([^/]+)/paged=/?([0-9]{1,})/?$', 'index.php?name=$matches[1]&paged=$matches[2]', 'top');
}

Then (after uploading functions.php) in WordPress admin panel:

Settings -> Permalinks -> [ Save Changes ]

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.