John Davidson

php - Timber - Pagination didn't work with default Wordpress query

0 comments
Message:


I'm trying to display pagination in my default query on blog page.
So I have file called home.php with this code


<?php

use Timber\Post;
use Timber\Timber;

$post = new Post();

$context = Timber::context();
$context['post'] = $post;

global $paged;
if (!isset($paged) || !$paged){
$paged = 1;
}

$args = [
'post_staus' => 'published',
'posts_per_page' => 2,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
'offset' => 1,
];
$context['posts'] = Timber::get_posts($args);

$argsNewest = [
'posts_per_page' => 1,
'offset' => 0,
];
$context['latest'] = Timber::get_posts($argsNewest);

Timber::render('page-home.twig', $context);

In the page-home.twig I'm trying to display my pagination but without any success.


{% include 'partial/pagination.twig' with { pagination: posts.pagination({show_all: false, mid_size: 3, end_size: 2}) } %}

The other weird thing is when I'm trying to dump posts like {{ dump(posts) }} it's not working however loop like {% for post in posts %} working fine and I can display posts.


So my questions:



  1. Why my pagination didn't work?

  2. Why my dump not working but loop yes?



My Answer:
In Timber, a WordPress plugin that allows you to use the Twig template engine in your theme, you can easily create pagination for your posts or custom post types. Here's how you can do it:

1. First, make sure you have Timber installed and set up in your theme.

2. In your template file where you want to display the pagination, add the following code:

twig
{% if posts.pagination %}

{% endif %}


3. This code checks if there are more pages to display and if so, it outputs the previous and next links for pagination.

4. You can customize the pagination links by adding classes or styling them as needed.

That's it! With just a few lines of code, you can easily add pagination to your Timber-powered WordPress theme.

Rate this post

3 of 5 based on 2096 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us