wordpresstwigtimber

Pagination in timber


I am using Timber, and currently trying to get pagination working as per the Timber docs, though so far, without success. Below is my custom archive php file:

global $paged;
if (!isset($paged) || !$paged){
    $paged = 1;
}
$context = Timber::get_context();
$args = array(
    'post_type' => 'product',
    'posts_per_page' => 2,
    'paged' => $paged,
);
$context['subCatItems'] = new Timber\PostQuery($args);

Timber::render('archive-product.twig', $context);

And my corresponding Twig file:

<div class="l-container vert-push--small vert-push--adult--medium">
    {% for post in posts %}
        <div class="c-std">{{ post.title }}</div>
    {% endfor %}
</div>

<div class="tool-pagination c-std">
    {% if posts.pagination.prev %}
        <a href="{{posts.pagination.prev.link}}" class="prev {{posts.pagination.prev.link|length ? '' : 'invisible'}}">Prev</a>
    {% endif %}
    <ul class="pages">
        {% for page in posts.pagination.pages %}
            <li>
                {% if page.link %}
                    <a href="{{page.link}}" class="{{page.class}}">{{page.title}}</a>
                {% else %}
                    <span class="{{page.class}}">{{page.title}}</span>
                {% endif %}
            </li>
        {% endfor %}
    </ul>
    {% if posts.pagination.next %}
        <a href="{{posts.pagination.next.link}}" class="next {{posts.pagination.next.link|length ? '' : 'invisible'}}">Next</a>
    {% endif %}
</div>

The main issue at the moment is that posts_per_page seems to do nothing, and I am just getting all the posts rendering on the page (about 20 currently), when there should be only 2 with the pagination (which isn't currently showing. Any ideas?


Solution

  • Unfortunately I don't have the privilege to comment so I have to post this way.

    Your code shows that you render 'archive-product.twig' and query your 'product' post type so you you should try the approach of using the active query according to the docs you linked to. Did you try that already?

    Edit

    A while after posting this I needed to build an implementation of a timber twig pagination - and failed in the beginning because the item count was different to the number of posts per page as defined in WP backend. I thought to add a tiny bit of information that could be useful to somebody..

    So in case you need a different number of posts in you custom posttype archive having pagination try this:

    In your controller or you archive file:

    global $paged;
    if (!isset($paged) || !$paged){
        $paged = 1;
    }
    
    $args_cp = array(
        'post_type' => 'HERE_YOUR_CUSTOM_POSTTYPE_NAME',
        'posts_per_page' => 2,
        'paged' => $paged
    );
    $context['posts'] = new Timber\PostQuery($args_cp);
    

    Additionally you need this in your function.php as long as the number of posts per page is not the same like in the WP backend:

    function cpt_archive_posts_per_page( $query ) {
    
        // for cpt or any post type main archive
        if ( $query->is_main_query() && ! is_admin() && is_post_type_archive( 'HERE_YOUR_CUSTOM_POSTTYPE_NAME' ) ) {
            $query->set( 'posts_per_page', '2' );   // number has to be the same like in your query / 'posts_per_page'
        }
    }
    add_action( 'pre_get_posts', 'cpt_archive_posts_per_page' );
    

    If you get a 404 on page 2 try Timber Twigs authors suggestion: https://timber.github.io/docs/guides/pagination/

    Section: Pagination with pre_get_posts

    function my_home_query( $query ) {
        if ( $query->is_main_query() && !is_admin() ) {
            $query->set( 'post_type', array( 'movie', 'post' ));
        }
    }
    add_action( 'pre_get_posts', 'my_home_query' );
    

    Adjust values to your needs of course :)