HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux wordpress-ubuntu-s-2vcpu-4gb-fra1-01 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/delta/wp-content/themes/delta/vendor/timber/timber/docs/guides/pagination.md
---
title: "Pagination"
menu:
  main:
    parent: "guides"
---

Do you like pagination? Stupid question, of course you do. Well, here's how you do it.

## Default pagination

This will only work in a php file with an active query (like `archive.php` or `home.php`):

```php
	<?php
	$context = Timber::context();
	$context['posts'] = new Timber\PostQuery();
	Timber::render('archive.twig', $context);
```

You can then markup the output like so  (of course, the exact markup is up to YOU):

```twig
<div class="tool-pagination">
	{% 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>
```

## What if I'm not using the default query?

```php
	<?php
	global $paged;
	if (!isset($paged) || !$paged){
		$paged = 1;
	}
	$context = Timber::context();
	$args = array(
		'post_type' => 'event',
		'posts_per_page' => 5,
		'paged' => $paged
	);
	$context['posts'] = new Timber\PostQuery($args);
	Timber::render('page-events.twig', $context);
```

## Pagination with pre_get_posts

Custom `query_posts` sometimes shows 404 on example.com/page/2. In that case you can also use `pre_get_posts` in your functions.php file:

```php
	<?php
	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' );
```
In your archive.php or home.php template:

```php
	<?php
	$context = Timber::context();
	$context['posts'] = new Timber\PostQuery();
	Timber::render('archive.twig', $context);
```