3

I'm using this function right now

wp_link_pages( array('before' => '<div class="page-links">','after'  => '</div>',) );

The post pagination is showing like this:

1 2 3 4 5 6 7 8 9 10 11

But I want it to be like this in that there's too many pages

1 2 3 ..9 10 11

However, There's no mid_size or end_size like function paginate_links()

I've been searching for answer all day, Is there anyone could help?

2 Answers 2

0

It's not possible with wp_link_pages(), but you can use paginate_links(). You just need to configure the arguments so that the links are based on the pagination of your post/page. To do this you just need to know:

  • The base URL. This is the permalink of the page/post.
  • The format of the pagination portion of the URL. This is just the page number, like /6, and not /page/6 or /?page=5 or anything like that.
  • The total number of pages. This is the global $numpages variable.
  • The current page. This is the global $page variable.

Which you can use like this:

echo paginate_links(
    [
        'base'    => user_trailingslashit( trailingslashit( get_the_permalink() ) . '%_%' ),
        'format'  => '%#%',
        'total'   => $numpages,
        'current' => $page,
    ]
);

Note that we want base to be something like this, where %_% is where our page number will go:

http://website.test/2021/03/25/my-post/%_%/

To do this properly we use trailingslashit() on get_the_permalink() to guarantee that there is a slash between the URL and the placeholder. Then we use user_trailingslashit() to add a trailing slash to the very end in case the site is configured that way.

7
  • Would this change the pagination to use 1,2,3,....8,9,10 though? I don't think the OP wants to change the URL
    – Tom J Nowell
    Commented Mar 25, 2021 at 15:05
  • Yes it would, and it’s not changing the URL. Commented Mar 25, 2021 at 15:45
  • I've re-read the answer and I don't understand how it achieves the OP's desired output, or what base URLs have to do with it. I think there's an important piece of information you've assumed is obvious that is not.
    – Tom J Nowell
    Commented Mar 25, 2021 at 21:29
  • What aren’t you getting? OP is using wp_link_pages, which is to show page links when using <!--nextpage-->. They want to use the features of paginate_links(). This code shows how you can use paginate_links() for <!--nextpage--> pagination. Commented Mar 26, 2021 at 0:50
  • Thanks for the answer,but It's not working.Maybe is because my post URL is like this example.com/story/10981/page-2.html example.com/story/10981/page-3.html
    – zachary
    Commented Mar 26, 2021 at 10:23
0

Jacob is right that you need to incorporate the function of paginate_links() but I would suggest looking at the examples on this page:

https://developer.wordpress.org/reference/functions/get_the_posts_pagination/

My Theme (the amazing MHMagazine Theme) has this function built in and this is how they do it (it does exactly as OP wants, a few digits, an ellipsis, a few more digits, and right or left arrows as needed):

if (!function_exists('mh_magazine_pagination')) {
function mh_magazine_pagination() {
    if (get_the_posts_pagination()) {
        echo '<div class="mh-loop-pagination clearfix">';
            the_posts_pagination(array(
                'mid_size' => 1,
                'prev_text' => esc_html__('&laquo;', 'mh-magazine'),
                'next_text' => esc_html__('&raquo;', 'mh-magazine'),
            ));
        echo '</div>';
    }
}
}

You can see an example of this in action at this page (scroll to bottom):

https://www.bestallinclusive.com/category/insider-tips/

(Mods, it's okay to delete that last link if not allowed, but I think this does exactly what OP wants so thought he'd like to see it to be sure before trying to implement this).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.