2
votes

I have a CPT ('vehicles') with custom taxonomy ('vehicles_category')

I have 4 categories (construction, road, waste, other) with numerous posts assigned to each of these categories.

I'm trying to list out each category with the posts assigned to them underneath so..

Construction
     Post 1    
     Post 2     
     etc
Road
     Post 6
     Post 9
     etc

Been battling with this.

I have this to get the posts...

$context['vehicles'] = Timber::get_posts([
    'order'     => 'ASC',
    'post_type' => 'vehicles',
    'posts_per_page' => -1,
    'paged' => $paged,
]);

and this to get the categories...

$context['categories'] = Timber::get_terms('vehicle_category');
$context['posts'] = $context['vehicles'];

So far i have this in my twig file...

{% for category in categories %}
    {% if category.count >= 1 %}
        <li>
            <a href="{{site.url}}/vehicles/{{ category.slug }}">{{ category.title }}</a>
        </li>
    {% endif %}
{% endfor %}

But it only outputs the categories as I'm missing the bit to output the posts but everything I try doesn't work.

Any ideas? Thanks

2

2 Answers

4
votes

Ok, really overthought this so stepped back a bit.

$context['categories'] = Timber::get_terms('vehicle_category');

vehicle_category is my custom taxonomy.

{% for category in categories %}
    <h1>{{category.name}}</h1>

    <div class="c-vehicle--list">

                {% for post in category.posts %}
                    <div class="c-vehicle--list__item">

                            <a href="{{ site.url }}/vehicles/{{ post.slug }}" title="{{ post.title }}">
                                <img src="{{TimberImage(post.vehicle_image).src('postMedium')}}" alt="{{ post.title }}">
                                <span>{{ post.title }}</span>
                                <span>{{ post.terms('vehicle_category') | join(', ') }}</span>
                            </a>

                    </div>
                {% endfor %}
    </div>
{% endfor %}

So simply loop out the categories in my custom taxonomy and then list the posts post in category.post associated with that category.

0
votes

Its been already asked here Get all categories and posts in those categories

Anyway, here is another code that will help you achieve same result:

<?php
    //for each category, show all posts
    $cat_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) {
        $args=array(
          'showposts' => -1,
          'category__in' => array($category->term_id),
          'caller_get_posts'=>1
        );
        $posts=get_posts($args);
          if ($posts) {
            echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
            foreach($posts as $post) {
              setup_postdata($post); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>

Source: List all categories with posts