Support » Developing with WordPress » WordPress default gallery pagination when call is in page (is_page)?

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

    When gallery call is in page, it works only if URL is like this:, 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 , 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_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?

    • This topic was modified 1 week, 3 days ago by bonmot.
    • This topic was modified 1 week, 3 days ago by bonmot.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    Added rewrite rules can only match either a page or a post request. They cannot be ambiguous like the default post/page URL can. This is because you must either use the “name” or “pagename” query var, committing to one post type or the other. If you want custom pagination URLs for both types, there needs to be something distinct about each URL, for example: match “paging” for one type and “pagenum” for the other. So you’d need two different rewrite rules to accommodate both. Naturally, the links output by paginate_links() would need to also be similarly distinct for each type.

    Thread Starter bonmot


    Less ambitious goal first. For the time present I try to master control over pagination just when gallery call is in page. And after that I will have to think how to combine them both: call in page + call in post.

    When gallery call is in page, I can only make it work if URLs are like,, but not any other way. In contrast to when the call is in post, where the structure of URL is controllable.

    I have tried to use pagename instead of name in index.php?name=$matches[1]&paged=$matches[2], but still I can not make it work. When I click on page 2 link, it goes to the wrong URL: and displays wrong content (from one of the posts).

    • This reply was modified 1 week, 1 day ago by bonmot.
    Moderator bcworkz


    Sounds like a different rule is being matched. Either there’s a superseding rule further up (unlikely since you used 'top') or your regexp failed to properly match the URL. You can test your regexp in sites like On that site you do need to escape slashes because of the delimiters used, but WP rules use a different delimiter, so escaping is optional.

    There is one other possible way for added rules to fail to match: you forgot to flush the current rules. You can do so by visiting the permalinks settings screen.

    “name” is the correct query var for “post” post type slugs. “pagename” of course then is correct for “page” post type slugs. They are not interchangeable. You’ll need two distinct rules to cover both types. The latter rule should not be possibly matched by the former, which is why I suggested two different URL paging elements.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.