如何使用Twig和October CMS从博客列表中排除已知类别

问题描述 投票:0回答:1

我正在尝试从博客列表中排除已知类别,因为我在页面的其他位置有一个特殊的小部件,并且希望避免我的文章出现两次。这些文章可以分为两类,这就是为什么我有问题。

我的逻辑是首先检查文章上是否设置了类别,这就是为什么我在代码中编写了两次小部件的原因。然后,我想有一个if语句来检查我的类别名称(或id或可以用来追踪这只猫的任何东西)是否在我的文章的category.list中。

我确实设法只隐藏了一个类别的文章,但是由于这些文章很可能会包含两个不同的类别,所以我完全迷失了。

设置完整的类别列表会以某种方式帮助我吗?

如果您知道一些魔术树枝的咒语可以帮助我,请告诉我。

<div class=main-layout-header></div>
<div class=main-layout-content>
    <h2 class="">All the News</h2>
    {% set posts = blogList.posts %}
        <div class="post-list">
            {% for post in posts %}
                {% if post.categories is not empty  %}
                    <div id="" class="news-card-layout category defined">
                        {% if post.image %}
                            <div class="news-card-layout__image">
                                <a href="{{ post.url }}"><img src="{{ post.image|media }}" alt="{{ post.title }}"></a>
                            </div>
                        {% endif %}
                        <div class="news-card-layout__details">
                        <div class="news-card-layout__infos">
                            <a href="{{ post.url }}"><h2 class="post-title" >{{ post.title }}</h2></a>
                            Posted
                            {% if post.categories.count %} in {% endif %}
                            {% for category in post.categories %}
                            <a href="{{ category.url }}">{{ category.name }}</a>{% if not loop.last %}, {% endif %}
                            {% endfor %}
                            on {{ post.published_at|date('Y-m-d G:i') }}
                        </div>
                        <div class="news-card-layout__excerpt">{{ post.summary|raw }}</div>
                    </div>
                </div>      

                {% else %}
                <div id="" class="news-card-layout category not defined">
                    {% if post.image %}
                        <div class="news-card-layout__image">
                            <a href="{{ post.url }}"><img src="{{ post.image|media }}" alt="{{ post.title }}"></a>
                        </div>
                    {% endif %}
                    <div class="news-card-layout__details">
                        <div class="news-card-layout__infos">
                            <a href="{{ post.url }}"><h2 class="post-title" >{{ post.title }}</h2></a>
                            Posted
                            {% if post.categories.count %} in {% endif %}
                            {% for category in post.categories %}
                            <a href="{{ category.url }}">{{ category.name }}</a>{% if not loop.last %}, {% endif %}
                            {% endfor %}
                            on {{ post.published_at|date('Y-m-d G:i') }}
                        </div>
                        <div class="news-card-layout__excerpt">
                            {{ post.summary|raw }}
                        </div>
                    </div>
                </div>
            {% endif %}
        {% endfor %}
    </div>

</div>
twig octobercms
1个回答
0
投票

为什么不测试类别的长度(或者在这种情况下,树枝将计算数组中的数量)。

{% if post.categories|length == 1 %}

-EDIT-

组件本身也可以排除类别。看这个。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.