timber / starter-theme

The "_s" for Timber: a dead-simple theme that you can build anything from
MIT License
817 stars 276 forks source link

How i can set how many posts per page are displayed in the archive section #62

Closed matiasbenedetto closed 6 years ago

matiasbenedetto commented 6 years ago

Hello, i want to change the quantity of posts displayed in the archive section, but i dont know how. If i use this snippet in the end of the archive.php file i get the correct number of posts in pagination but the posts are the same no matter the category i selected previously.

        if (!isset($paged) || !$paged){
        $paged = 1;
    }
    $args = array(
        'posts_per_page' => 8,
        'paged' => $paged
    );

$context['posts'] = new Timber\PostQuery($args);
$context['pagination'] = Timber::get_pagination();
Timber::render( $templates, $context );

if i run Timber\PostQuery() without arguments i get the correct posts of one category or search result, but the quantity in the pagination is not which i need. $context['posts'] = new Timber\PostQuery();

75th commented 6 years ago

The easy and safe answer for this one question is that in Settings > Reading, there's an option for number of posts per page, which will affect all queries automatically.

But to solve the general problem of overriding individual aspects of the default query, this is what I've found. I'm not that great with WordPress, so this might be dangerous or bad in some way; I'd love for someone to tell me if so.

$query = $GLOBALS['wp_query']->query_vars;
$query['posts_per_page'] = 8;
$context['posts'] = new Timber\PostQuery($query);
$context['pagination'] = $context['posts']->pagination([
    'show_all' => false,
    'mid_size' => 3,
    'end_size' => 2
]);
jarednova commented 6 years ago

@75th couldn't have said it better myself!