4

I'm working with the paginate_links() function and I'm trying to create a pagination in this kind of style;

< 3 4 5 6 7 >

Logically I thought it would be simple by setting end_size to 0 and mid_size to 2.

I have done this but my code is outputting as this;

< 1 ... 3 4 5 6 7 ... 9 >

To me, this is showing the end_size as 1, even though I have it set to 0 (unless I'm misunderstanding what the option does).

My current paginate links function looks like this;

$paginationArgs = array(
    'base'          => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format'        => '?paged=%#%',
    'current'       => max( 1, get_query_var('paged') ),
    'total'         => $loop->max_num_pages,
    'end_size'      => 0,
    'mid_size'      => 1,
    'prev_next'     => TRUE,
    'prev_text'     => '<',
    'next_text'     => '>',
    'type'          => 'list',
    'add_args'      => FALSE,
    'add_fragment'  => ''
)
$pagination = paginate_links($paginationArgs);

Does anyone have any advice/answers?

Thanks, Harry.

5

Here's one suggestion:

First we need to let paginate_links() return an array instead of a HTML list:

'type' => 'array',

Then we can grab the output with:

$paginate_links = paginate_links( $paginationArgs );

The plan is then to filter out the wanted links.

Let's get the current value:

$c = $paginationArgs['current'];

We construct the search filter as:

$allowed = [
    ' current',
    'prev ',
    'next ',
    sprintf( '/page/%d/', $c-2 ),
    sprintf( '/page/%d/', $c-1 ),
    sprintf( '/page/%d/', $c+1 ),
    sprintf( '/page/%d/', $c+2 )
];

Note that we could refine this around the edges and only filter what's available. It's also possible to make this dynamically depend on the mid_size attribute. Here we assume that:

'mid_size' => 2,

Also note that:

'end_size' => 0,

means that end_size is 1 because of the following check in paginate_links() core function:

if ( $end_size < 1 ) {
    $end_size = 1;
}

Then we filter the allowed paginate links:

$paginate_links = array_filter(
    $paginate_links,
    function( $value ) use ( $allowed ) {
        foreach( (array) $allowed as $tag )
        {
            if( false !== strpos( $value, $tag ) )
                return true;
        }
        return false;
    }
);

Finally we display the outcome:

if( ! empty( $paginate_links ) )
    printf(
        "<ul class='page-numbers'>\n\t<li>%s</li>\n</ul>\n",
        join( "</li>\n\t<li>", $paginate_links )
    );

Hope you can adjust it to your needs!

|improve this answer|||||
  • Wow thanks, this worked great. I did have to do a couple of edits just for anyone referencing this in the future; On this line: foreach( (arrow) $allowed as $tag ), the (arrow) bit caused it to error (I'm not sure what it's supposed to do but it's working without it. Also, on the final piece of code the $page_links should be $paginate_links (or whatever you set your paginate_links() function to be). Thanks again for your help! – Harry Francis Sep 1 '16 at 10:01
  • There is one small thing i've just noticed, I missed it in my original question so my bad. The next/previous arrows should go to the first/last page rather than the next/previous pages. Is this an easy change or should I just add them in statically using page 1/max_num_pages? – Harry Francis Sep 1 '16 at 10:16
  • Glad to hear it worked for you. Thanks for mentioning the typos, I updated the answer I was just reading about arrow functions in javascript but meant to write array here ;-) I remember thinking about various workarounds in another paginate_links() problem here. but ended up doing this kind of filtering approach, as it seemed simple to work with. Not sure what you mean exactly regarding the prev/next but adding them statically might work as well. @HarryFrancis – birgire Sep 1 '16 at 11:38
  • Ah, I will make that change to mine also, thanks. It's a nice method as it gives you good options with the output! Currently the next/previous links go to the next/previous page from the current one you're on. I'm hoping to get it so they go to first/last page instead. – Harry Francis Sep 1 '16 at 12:20

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.