0

I am currently using this code in my WordPress/Timber index.php file to select posts from single categories. I am needing to expand it so posts that have multiple categories checked will be selected and displayed in the loop. The parent category is "stories" and "camping-america,canada,united-states,world" are the children categories. I used this Timber Wordpress - Show blocks of posts from two categories question to implement this code.

PHP in index.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;

Twig code

{% for post in posts.stories %}
   {% include ['tease-stories.twig'] %}
{% endfor %}
Mike Hermary
  • 346
  • 7
  • 22

0 Answers0