0
votes

I'm trying to use Timber's pagination features, but the links being rendered just send me to a 404 page. Here is how I'm getting the list of posts:

$posts = new Timber\PostQuery([
  'post_type' => 'event',
  'paged' => $paged,
  'posts_per_page' => 6,
]);
$context['posts'] = $posts;

And how I'm rendering them:

{% if posts.pagination.pages is not empty %}
  <nav class="navigation" role="navigation">
    <ol class="pagination">
      {% if posts.pagination.prev %}
        {% include "wp/pagination/_pagination-link.twig" with {"class": posts.pagination.prev.class, "link": posts.pagination.prev.link, "title": "&larr;"} %}
      {% endif %}

      {% for page in posts.pagination.pages %}
        {% include "wp/pagination/_pagination-link.twig" with {"class": page.class, "link": page.link, "title": page.title} %}
      {% endfor %}

      {% if posts.pagination.next %}
        {% include "wp/pagination/_pagination-link.twig" with {"class": posts.pagination.prev.class, "link": posts.pagination.next.link, "title": "&rarr;"} %}
      {% endif %}
    </ol><!-- /.pagination -->
  </nav><!-- /.navigation -->
{% endif %}

When I visit the page, the URL path is /event/ and when I click the second/next page button, it links to /event/page/2/, but Wordpress doesn't run the same php file (archive-event.php).

How do I configure the pagination links? Can I switch them to put the page in the query like /event?page=2?

1

1 Answers

0
votes

The problem ended up having to do with rewrite rules. Wordpress was expecting pages with length 10 instead of 6. Adding this to functions.php fixed the issue:

function get_events($query)
{
    if(!is_admin() && $query->is_main_query() && is_post_type_archive('event')) {
        $query->set('posts_per_page', '6');
    }
}
add_action('pre_get_posts', 'get_events');