【问题标题】:How to exclude a known category from a blog list using Twig and October CMS如何使用 Twig 和十月 CMS 从博客列表中排除已知类别
【发布时间】:2020-01-23 12:25:37
【问题描述】:

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

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

我确实设法隐藏了只有一个类别的文章,但因为这些文章很可能有 2 个不同的类别,所以我完全迷失了。

设置完整的类别列表对我有什么帮助吗?

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

<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】:

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

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

    --编辑--

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

    【讨论】:

      猜你喜欢
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      相关资源
      最近更新 更多