bright-star
bright-star

Reputation: 6427

Can't enumerate through Jekyll pages by tag

goal

Generate a page list filtered by a single tag.

problem

List doesn't appear.

things I tried

I started with the <nav> element in the default Jekyll site:

<nav class="site-nav">
<div class="trigger">
  {% for page in site.pages %}
    {% if page.title %}
      <a class="page-link" href="{{ page.url | prepend: site.baseurl }}">{{ page.title }}</a>
    {% endif %}
  {% endfor %}
</div>
</nav>

When the Jekyll site is served, this puts an "About" page in the nav element.

I added two tags to different pages' front matter on the site:

tags:
- eng
---

I tried both formats:

tags: eng
---
  1. Try enumerating through site.tags.eng (also tried site.tags[eng] and site.tags["eng"]):

    <div class="trigger"> {% for page in site.tags.eng %} {% if page.title %} <a class="page-link" href="{{ page.url | prepend: site.baseurl }}">{{ page.title }}</a> {% endif %} {% endfor %} </div>

  2. Try testing for a tag (with or without quotes):

    <div class="trigger"> {% for tag in site.tags %} {% if tag == "eng" %} {% if page.title %} <a class="page-link" href="{{ page.url | prepend: site.baseurl }}">{{ page.title }}</a> {% endif %} {% endif %} {% endfor %} </div>

  3. Also add a tag list to _config.yml:

    # Tags
    tags: [eng, jpn]
    
  4. Add a tag to the sample post to see if only posts are being indexed

other questions I looked at

Upvotes: 3

Views: 196

Answers (1)

David Jacquel
David Jacquel

Reputation: 52779

Settings tags on a page :

tags: [ tag1, tag3 ]
---
or
tags:
 - tag1 
 - tag2
---

If you want to make a list from a tag :

<ul>
  {% for p in site.pages %}
    {% if p.tags contains 'tag2' %}
    <li><a href="{{ p.url | prepend: site.baseurl }}">{{ p.title }}</a></li>
    {% endif %}
  {% endfor %}
</ul>

Secondary answers :

  • It's normal for about page to appear in pages list (default menu) it's a page
  • Tagged pages will not appear in site.tags hash, only posts.

Upvotes: 2

Related Questions