0

It seems I have found the way to make WordPress default gallery pagination to work and to have control over URL forming: Pagination with WordPress default gallery? But when gallery call is in post.

When gallery call is in page, it works only if URL is like this: domain.com/pagename/page/2/, domain.com/pagename/page/3/ etc. Trying to have a different URL ends up in stopping it to work.

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;')
) );

As long as I keep URL like this domain.com/pagename/page/2/ , other ways for 'base' also work:

'base' => get_permalink( $post->post_parent ) . '%_%',
'format' => 'page/%#%/',

And like this:

'base' => get_pagenum_link( 1 ) . '%_%',
'format' => 'page/%#%/',

But when I try some other URL scheme:

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

pagination stops working.

The 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('(.+)/paging-/?([0-9]{1,})/?$', 'index.php?name=$matches[1]&paged=$matches[2]', 'top');
}

It seems like add_rewrite_rule is not functioning when gallery call is in page? Or what? Any ideas?

1 Answer 1

0

Probably because in your rewrite rule index.php?name=$matches[1]&paged=$matches[2], name is a query var for post slugs - if you want to match pages you need pagename.

1
  • I have tried it right now: using pagename instead of name. It does not seem to help. When I click on page 2 link, it goes to some strange and wrong URL: domain.com/pagename/paging-2/page/2/ and displays wrong content (from one of the posts).
    – BonMoDo
    Jun 15 at 16:32

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.