skip to Main Content

I am using version 2 of Timber and I am now adding pagination to my archives.

Timber, by default, returns 9 pages in the array for pagination. However, I’d rather only want to show first page, the last one and in between the current one with 2 surrounding pages.

The situation as I would like to see it:
For page 1:
1 | 2 | 3 | ... | 300 | Next >

For example page 7:
< Back | 1 | ... | 5 | 6 | 7 | 8| 9 | ... | 300 | Next >

However the default of Timber (at least for V2) is:
For page 1:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ... | 300 | Next >

For page 7:
< Back | 1 | ... | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ... | 300 | Next >

Because the array with post.pagination.pages always contains 9 entries.

I am using the following pagination.twig for that in my templates.

{% if posts.pagination.pages is not empty %}
    <nav class="pagination" role="navigation" aria-label="pagination">
        <ol class="pagination__items">
            {% if posts.pagination.prev %}
                <li class="pagination__previous pagination__link">
                    <a href="{{ pagination.prev.link }}">&laquo; {{ __( 'Vorige', 'my-site' ) }}</a>
                </li>
            {% endif %}

            {% for page in posts.pagination.pages %}
                <li class="pagination__link {{ page.class }}">
                    {% if page.link %}
                        <a href="{{ page.link }}">
                            <span class="visually-hidden">{{ __( 'Pagina', 'my-site' ) }}</span> {{ page.title }}
                        </a>
                    {% else %}
                        <span>
                            <span class="visually-hidden">{{ __( 'Pagina', 'my-site' ) }}</span> {{ page.title }}
                        </span>
                    {% endif %}
                </li>
            {% endfor %}

            {% if posts.pagination.next %}
                <li class="pagination__next pagination__link">
                    <a href="{{ pagination.next.link }}">{{ __('Volgende', 'my-site') }}&raquo;</a>
                </li>
            {% endif %}
        </ol>
    </nav>
{% endif %}

I tried finding a good filter or action to change the settings for this, but unfortunately I can’t seem to find one. It looks like version 1 did support this. with pagination() though. However I can’t find an alternative for this for V2.

2

Answers


  1. Chosen as BEST ANSWER

    The answer by Radek put me on the right trail

    use TimberPagination;
    
    $context['pagination'] = Pagination::get_pagination( [
        'mid_size' => 2,
        'end_size' => 1,
        'show_all' => false,
    ] );
    

  2. According to the docs:

    https://timber.github.io/docs/v2/reference/timber-pagination/#get_pagination

    you could easily pass the arguments here so those are from WordPress docs:

    https://developer.wordpress.org/reference/functions/paginate_links/#parameters

    Finally you can use that like this:

    use TimberPagination;
    
    ...
    
    $context['pagination'] = Pagination::get_pagination([
        'mid_size' => 1,
        'end_size' => 1,
        'show_all' => false,
    ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search