我目前正在WordPress / Timber index.php文件中使用此代码从单个类别中选择帖子。我需要对其进行扩展,以便选中具有多个类别的帖子并将其显示在循环中。父类别是“故事”,“野营美国,加拿大,美国,世界”是子类别。我用这个Timber Wordpress - Show blocks of posts from two categories问题来实现此代码。
index.php中的PHP
$query = array(
'category_name' => 'stories,camping-america,canada,united-states,world',
);
$posts = Timber::get_posts( $query );
$sorted_posts = array();
foreach ( $posts as $post ) {
// Get first category of post
$category = $post->category();
// Fill post back to sorted_posts
$sorted_posts[ $category->slug ][] = $post;
}
$context['posts'] = $sorted_posts;
树枝代码
{% for post in posts.stories %}
{% include ['tease-stories.twig'] %}
{% endfor %}