OG Altmayer
OG Altmayer

Reputation: 35

How to exclude a known category from a blog list using Twig and October CMS

I'm trying to exclude a known category from my blog list, as I have a special widget for it elsewhere on my page and want to avoid my articles appearing twice. Those articles can have 2 categories, which is why I have an issue.

My logic was to first check if the categories were set on the articles, which is why I have the widget written twice on the code. I then wanted to have an if statement to check whether or not my category name (or id, or anything that can be used to track down this cat) was in the categories.list of my article.

I did manage to hide the articles with only one category, but because those articles will most likely have 2 different ones, I am completely lost.

Would setting the full category list help me somehow?

If you know of some magic twig incantations to help me, please let me know.

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

Upvotes: 0

Views: 329

Answers (1)

Pettis Brandon
Pettis Brandon

Reputation: 865

Why not test for length (or in this case twig will count the amount in the array) of categories.

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

--EDIT--

Also the component itself can exclude a category. Look at this.

enter image description here

Upvotes: 2

Related Questions