Getting paginate_links() ‘end_size’ to display none

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 Comment