lesterchan / wp-pagenavi

Adds a more advanced paging navigation interface to your WordPress blog.
https://wordpress.org/plugins/wp-pagenavi/
134 stars 49 forks source link

Using WP-Page with Timber #52

Closed palmiak closed 6 years ago

palmiak commented 7 years ago

It's not an issue, but information that maybe should be added to documentation / FAQ.

I had some problems with getting to WP_PageNavi to work with Timber and custom queries, as it turned out the solution was quite simple and it didn't required any extra queries.

// your query args
$args = [
    'posts_per_page' => $context['config']['posts_per_page'],
    'post__not_in' => $ids,
    'paged' => $paged,
];

// WP query
$posts_wp = new WP_Query( $args );

// passing data for Timber
$posts = Timber::query_posts( $posts_wp );

// setting WP_PageNavi
$context['wp_pagenavi'] = wp_pagenavi(
    [
        'echo' => false,
        'query' => $posts_wp,
    ]
);

than it's just passing inserting {{ wp_pagenavi }} in our template.