【问题标题】:Timber - list categories and their posts木材 - 列出类别及其帖子
【发布时间】:2018-04-26 19:21:43
【问题描述】:

我有一个带有自定义分类法('vehicles_category')的 CPT('vehicles')

我有 4 个类别(建筑、道路、废物、其他),每个类别都有许多帖子。

我正在尝试在下面列出每个类别以及分配给它们的帖子......

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

一直在与这个作斗争。

我有这个来获取帖子...

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

这是获取类别...

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

到目前为止,我的树枝文件中有这个...

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

但它只输出类别,因为我缺少输出帖子的位,但我尝试的一切都不起作用。

有什么想法吗? 谢谢

【问题讨论】:

    标签: wordpress categories timber


    【解决方案1】:

    好吧,确实是想多了,所以退后了一点。

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

    vehicle_category 是我的自定义分类。

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

    所以只需循环出我的自定义分类中的类别,然后列出与该类别相关的帖子post in category.post

    【讨论】:

      猜你喜欢
      • 2016-07-24
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多