HufflepuffBR
HufflepuffBR

Reputation: 463

Jekyll - Grouping some itens into a category by tags

First of all, scenario:

I have some documents into a category X, that have tags Y,Z,W.

I want to group then so, I can do something like this into a document

Any tip?

Tried this code

<ul>
    {% for tag in site.categories.personagens.tags %}
    <li/> {{ tag }}
    <ul/>
    {% for posts in tag %}
    <li/><a href="{{ post.url }}">{{ post.title | markdownify | remove: '<p>' | remove: '</p>' }}</a>
    {% comment %}
    {{ post.content }}
    {% endcomment %}
    {% endfor %}
    {% endfor %}
</ul>

But it didn't worked...

Upvotes: 1

Views: 342

Answers (1)

David Jacquel
David Jacquel

Reputation: 52799

You can try this :

{% comment %}Posts will be filtered by one category{% endcomment %}
{% assign filterCategory = "works" %}

{% for tag in site.tags %}
    {% comment %}creates an empty array{% endcomment %}
    {% assign postsInCategory = "" | split: "/" %}

    {% comment %}looping over site.tags{% endcomment %}
    {% for post in tag[1] %}
        {% if post.categories contains filterCategory %}
            {% comment %}if a post is from our filter category we add it to postsInCategory array{% endcomment %}
            {% assign postsInCategory = postsInCategory | push: post %}
        {% endif %}
    {% endfor %}

    {% if postsInCategory.size > 0 %}
        <h1>{{ tag[0] }}</h2>
        {% for post in postsInCategory %}
            <h2><a href="{{ site.baseurl }}{{ post.url }}"></a>{{ post.title }}</h2>
        {% endfor %}
    {% endif %}
{% endfor %}

Upvotes: 1

Related Questions