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.

1
1

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!

Leave a Reply

Your email address will not be published. Required fields are marked *