全球分页重写网址

Update

Tested my script outside Wordpress and it works. So the problem with this pagination is inside Wordpress. I did some research and I added this rewrite code to my functions file, but still it doesn't work

function my_rewrite_globe_pagination()
{
 add_rewrite_rule('^newspaper/page/([0-9]{1,})/?', 'index.php?paged=$matches[1]', 'top');
}

add_action('init', 'my_rewrite_globe_pagination');

The next button url looks like this: domain/page-name/?page=2
URL in browser after clicking next: domain/page-name/page/2/

Before update
first of all I would like to say I read all the similar questions on stackoverflow but I din't find the solution fitting to my case.

What I'm trying to achieve is a globe numeric pagination at my wordpress page. In my page template I have the code listed below and I have with this code two problems:

  • one is that I don't know how to change the prev and next buttons to numeric pagination
  • second whenever I click on the next button I see the same two images as on first page, despite the fact that my url changes to domain/page-name/2/
 if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }

        $base_dir = trailingslashit(wp_upload_dir()['basedir']);
        $base_url = wp_upload_dir()['baseurl'];
        $dir_jpg  = '/newspaper/jpg/';
        $dir_pdf  = '/newspaper/pdf/';

        $images = glob($base_dir.$dir_jpg.'*.*');

        $limit = 2;
        $total =count($images);
        $total_pages = ceil($total/$limit);
        $offset = ($page-1) * $limit;

        $images = array_slice($images, $limit);


        foreach($images as $image) {
          $url = $base_url.$dir_jpg.basename($image);

          $filename = substr(basename($image), 0, -4);

          $pdfs = $base_url.$dir_pdf.$filename.".pdf";

          printf('<a href="'.$pdfs.'" target="_blank" class="newspaper-link col-xl-3"><img src="%s" alt="'.$filename.'.pdf"><div class="newspaper-hover"></div></a>', esc_url($url));
        }


        ?>

        <ul class="pagination">
          <li class="<?php if($page <= 1){ echo 'disabled'; } ?>">
            <a href="<?php if($page <= 1){ echo '#'; } else { echo "?page=".($page - 1); } ?>">Prev</a>
          </li>
          <li class="<?php if($page >= $total_pages){ echo 'disabled'; } ?>">
            <a href="<?php if($page >= $total_pages){ echo '#'; } else { echo "?page=".($page + 1); } ?>">Next</a>
          </li>
        </ul>

For the first problem you simply have to print $page - 1 and $page + 1 instead "Prev" and "Next" (and check if the current page is the first or the last):

    <ul class="pagination">
      <li class="<?php if($page <= 1){ echo 'disabled'; } ?>">
        <a href="<?php if($page <= 1){ echo '#'; } else { echo "?page=".($page - 1); } ?>"><?php echo ($page <= 1) ? "X" : ($page - 1)?></a>
      </li>
      <li class="<?php if($page >= $total_pages){ echo 'disabled'; } ?>">
        <a href="<?php if($page >= $total_pages){ echo '#'; } else { echo "?page=".($page + 1); } ?>"><?php echo ($page >= $total_pages) ? "X" : ($page + 1)?></a>
      </li>
    </ul>

The second problem is caused by your incorrect call to array_slice. According to the documentation, the second parameter must be the start offset of the array ($offset in your case) and the third the length of the array you want ($limit), so this should fix it:

$images = array_slice($images, $offset, $limit);