designmode
designmode

Reputation: 163

Timber posts_per_page only showing one post on archive

I have an archive page that it not showing all results as expected. Changing the post_per_page argument to -1 on my query does allow all results to display, however when it is restricted to 5 posts, I only receive one result:

if (!isset($paged) || !$paged){
    $paged = 1;
}
$context = Timber::get_context();
$course_args = array(
    'post_type' => 'course',
    'posts_per_page' => 4,
    'paged' => $paged,
    'tax_query' => array(
        array(
            'taxonomy' => 'course-category',
            'field' => 'ID',
            'terms' => $cat
        )
    )
);
$context['pagination'] = Timber::get_pagination();
$context['post'] = Timber::get_posts($course_args);

Basic iteration in my view

{% for course in post %}
    <div class="col-md-6 px-3 py-5">
        //courseCard
    </div>
{% endfor %}

Any help would be greatly appreciated as now I have been looking at it so long that I am going around in circles.

Upvotes: 0

Views: 392

Answers (1)

Ihar Aliakseyenka
Ihar Aliakseyenka

Reputation: 14183

Try to use Timber\PostQuery

$context = Timber::get_context();
$course_args = array(
    'post_type' => 'course',
    'posts_per_page' => 4,
    'tax_query' => array(
        array(
            'taxonomy' => 'course-category',
            'field' => 'ID',
            'terms' => $cat // I guess this variable was created before
        )
    )
);
$context['posts']   = new Timber\PostQuery($course_args);
// Render twig

You don't need to create pagination as PostQuery takes care of that In your twig file

{% for course in posts %}
    <div class="col-md-6 px-3 py-5">
        //courseCard
    </div>
{% endfor %}

{% include 'partials/pagination.twig' with {pagination: posts.pagination} %}

Your pagination.twig should contain information about pagination

{{ dump(pagination) }} 

Upvotes: 1

Related Questions