2016-12-28 17 views
1

私はWordpressでTimber Pluginを使用しています。私は各カテゴリに4つの記事を表示するループを作成したいと思います。Timber 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を含む各ポストのプレビューです。

答えて

2

まず、StackOverflowでこれらの質問をしていただきありがとうございます(サポートQで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

1

それはで動作するタイプミスされることがあります。

{% for child_post in term.posts %} 

の代わり:

{% for child_post in terms.posts %} 
関連する問題