Jekyll按标签过滤类别和帖子

时间:2014-10-16 18:25:58

标签: jekyll liquid github-pages

在Jekyll中,我按标签过滤帖子,显示具有指定标签的帖子列表,如下所示:

---
layout: layout
---
<ul>
{% for post in site.posts %}
{% for tag in post.tags %}
{% if tag == page.tag %}

<li itemprop="name" itemprop=name>{{ post.title }}</li>

{% endif %}
{% endfor %}
{% endfor %}
</ul>

Burt我要做的是显示包含标签帖子和每个类别下列出的帖子的类别。


示例用例

第1类有帖子1,帖子2,帖子3。 第2类有帖子4,帖子5,帖子6。 第3类有第7个帖子

帖子1,2,4有标签Tag1。

标签页应显示类别1 +帖子1,帖子2.类别2 +帖子4。


在主页上,我会显示每个类别下所有帖子的类别,如下所示:

{% assign counter=0 %}
<main id="page" role="main">
{% for category in site.categories %}
<div class="col pane ui-layout-{% assign counter=counter | plus:1 %}" id="{{ page.id }}">
    <div class="colheader">
        <div class="bar"></div>
        <p class="lead">Leadin text</p>
        <h1>{{ category | first }}</h1>
    </div>
    <div class="colscroll unlock">
{% for posts in category %}
{% for post in posts %}
    <div class="colblock" itemscope itemtype="http://schema.org/BlogPosting">    
        <div class="excerpt">
            <time class="date" datetime="{{ page.date | '%Y-%m-%dT%H:%i:%S-08:00' }}" itemprop="datePublished">
                {{ page.date | date: '%B %d, %Y' }}
            </time>
        <h1>
            <a itemprop="url" href="{{ post.url }}" target="_blank" title="{{ post.title }}">
                <span itemprop="name" itemprop=name>{{ post.title }}</span>
            </a>
        </h1>                        
        <p itemprop="description">
            {{ post.excerpt }}..
        </p>
        <p class="link_tags">{{ page.tags }}</p>
        </div>
    </div>
{% endfor %}
{% endfor %}
    </div>
</div>
{% endfor %}
</main>

因此,在标记页面上,我尝试实现我想要的内容,但它返回所有类别和所有帖子,就像主页一样,而不是通过标记过滤。

---
layout: layout
---
<ul>
{% for post in site.posts %}
{% for tag in post.tags %}
{% if tag == page.tag %}


{% assign counter=0 %}
<main id="page" role="main">
{% for category in site.categories %}
<div class="col pane ui-layout-{% assign counter=counter | plus:1 %}" id="{{ page.id }}">
    <div class="colheader">
        <div class="bar"></div>
        <p class="lead">Leadin text</p>
        <h1>{{ category | first }}</h1>
    </div>
    <div class="colscroll unlock">

{% for posts in category %}
{% for post in post.tags %}
        <div class="colblock" itemscope itemtype="http://schema.org/BlogPosting">    
            <div class="excerpt">
                <time class="date" datetime="{{ page.date | '%Y-%m-%dT%H:%i:%S-08:00' }}" itemprop="datePublished">
                    {{ page.date | date: '%B %d, %Y' }}
                </time>
                <h1>
                    <a itemprop="url" href="{{ post.url }}" target="_blank" title="{{ post.title }}">
                        <span itemprop="name" itemprop=name>{{ post.title }}</span>
                    </a>
                </h1>                        
                <p itemprop="description">
                    {{ post.excerpt }}..
                </p>
                <p class="link_tags">{{ page.tags }}</p>
            </div>
        </div>
{% endfor %}
{% endfor %}
    </div>
</div>
{% endfor %}
</main>

{% endif %}
{% endfor %}
{% endfor %}
</ul>

我alsio尝试了这个,但它返回一个空白页

---
layout: layout
---
<ul>
{% for post in site.posts %}
{% for tag in post.tags %}
{% if tag == page.tag %}


{% assign counter=0 %}
<main id="page" role="main">
{% for category in tag.categories %}
<div class="col pane ui-layout-{% assign counter=counter | plus:1 %}" id="{{ page.id }}">
    <div class="colheader">
        <div class="bar"></div>
        <p class="lead">Leadin text</p>
        <h1>{{ category | first }}</h1>
    </div>
    <div class="colscroll unlock">

{% for posts in category %}
{% for post in tag.posts %}
        <div class="colblock" itemscope itemtype="http://schema.org/BlogPosting">    
            <div class="excerpt">
                <time class="date" datetime="{{ page.date | '%Y-%m-%dT%H:%i:%S-08:00' }}" itemprop="datePublished">
                    {{ page.date | date: '%B %d, %Y' }}
                </time>
                <h1>
                    <a itemprop="url" href="{{ post.url }}" target="_blank" title="{{ post.title }}">
                        <span itemprop="name" itemprop=name>{{ post.title }}</span>
                    </a>
                </h1>                        
                <p itemprop="description">
                    {{ post.excerpt }}..
                </p>
                <p class="link_tags">{{ page.tags }}</p>
            </div>
        </div>
{% endfor %}
{% endfor %}
    </div>
</div>
{% endfor %}
</main>

{% endif %}
{% endfor %}
{% endfor %}
</ul>

The live site template is up on Github。 (标签列表是页面顶部左侧的第二个圆形图标)

1 个答案:

答案 0 :(得分:3)

我认为这就是你要找的东西:

---
layout: default
---

<main id="page" role="main">
{% for category in site.categories %}
    {% assign counter=0 %}
    {% for posts in category %}
        {% for post in posts %}
            {% if post.tags contains page.tag %}
                {% assign counter=counter | plus:1 %}
            {% endif %}
        {% endfor %}
    {% endfor %}

{% if counter != 0 %}

<div class="col pane ui-layout-{% assign counter=counter | plus:1 %}" id="{{ page.id }}">
    <div class="colheader">
        <div class="bar"></div>
        <p class="lead">Leadin text</p>
        <h1>{{ category | first }}</h1>
    </div>
    <div class="colscroll unlock">
{% for posts in category %}
{% for post in posts %}
{% if post.tags contains page.tag %}
    <div class="colblock" itemscope itemtype="http://schema.org/BlogPosting">
        <div class="excerpt">
            <time class="date" datetime="{{ page.date | '%Y-%m-%dT%H:%i:%S-08:00' }}" itemprop="datePublished">
                {{ page.date | date: '%B %d, %Y' }}
            </time>
        <h1>
            <a itemprop="url" href="{{ post.url }}" target="_blank" title="{{ post.title }}">
                <span itemprop="name" itemprop=name>{{ post.title }}</span>
            </a>
        </h1>
        <p itemprop="description">
            {{ post.excerpt }}..
        </p>
        <p class="link_tags">{{ page.tags }}</p>
        </div>
    </div>
{% endif %}
{% endfor %}
{% endfor %}
    </div>
</div>
{% endif %}
{% endfor %}
</main>

我刚修改了您的主页代码。

编辑:添加了条件代码。仅打印包含具有当前标记的帖子的类别。