Reputation: 625
I use Timber plugin with Wordpress. I would like to create a loop which display 4 articles in each category.
publications.php
$context = Timber::get_context();
$post = new TimberPost();
$cat_id = wp_get_post_categories($post->ID);
$context['each_cat'] = Timber::get_posts(array('cat' => $cat_id[0], 'posts_per_page' => 4));
Timber::render( array( 'publications.twig', 'page.twig' ), $context );
publications.twig
{% for category in each_cat %}
<h2 class="title">{{category.name}}</h2>
<article class="article--box">
{% include "bloc_preview.twig" %}
</article>
{% endfor %}
The include bloc_preview.twig
is a preview of each post.
Upvotes: 1
Views: 2891
Reputation: 1734
First off, @jandon thanks for asking these on StackOverflow (instead of clouding-up the GitHub issues with support Qs). The easiest way to accomplish what you're looking for is to use the .posts
method on your term objects. Based on your example, you can do this all in Twig...
publications.twig
<h2>{{ post.title }}</h2>
<h3>Related Posts</h3>
{% for term in post.terms('category') %}
<h3>Related Posts in {{ term.name }}</h3>
<ul>
{% for child_post in term.posts(4) %}
<li><a href="{{ child_post.link }}">{{ child_post.title }}</a></li>
{% endfor %}
</ul>
{% endfor %}
Upvotes: 2
Reputation: 11
Might be a typo it works with:
{% for child_post in term.posts %}
instead of:
{% for child_post in terms.posts %}
Upvotes: 1