木材Wordpress显示每个类别的帖子

问题描述:

我使用木材插件与Wordpress。我想创建一个循环,在每个类别中显示4篇文章。木材Wordpress显示每个类别的帖子

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 %} 

的包括bloc_preview.twig是每个柱的预览。

首先,@jandon感谢您在*上提出这些问题(而不是使用支持问题来解决GitHub问题)。完成要查找的最简单方法是使用.posts method on your term objects。根据你的榜样,你可以做这一切的树枝...

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 terms.posts(4) %} 
     <li><a href="{{ child_post.link }}">{{ child_post.title }}</li> 
     {% endfor %} 
    </ul> 
{% endfor %} 
+0

谢谢。没有任何显示。我不知道为什么 – Jandon

可能是它与一个错字:

{% for child_post in term.posts %} 

代替:

{% for child_post in terms.posts %}