0
votes

I am working on a jekyll / gh-pages site. I'd like to build a side bar that lists the number of posts according to a CUSTOM tag. So i can sort posts using different yml elements. It works just fine using the tags yml element and this code

<h3>Activities By Topic</h3>
{% for tag in site.tags %}
{% assign t = tag | first %}
  {% for atag in site.data.tags %}
    {% if atag.slug == t %}
      <h5><a href="{{ site.baseurl }}/{{ atag.slug }}">{{ atag.name }}  
    {% endif %}
  {% endfor %} 
      ({{ tag | last | size }}) 
     </a></h5>
{% endfor %} 

But what i'd like is another block that is "Activities by Type" (we are trying to sort posts in different ways. I setup a topic-tag yml element and a `topic-tag.yml file'

_data folder https://github.com/lwasser/data-lesson-catalog/blob/gh-pages/_data/topic-tags.yml org folder: https://github.com/lwasser/data-lesson-catalog/tree/gh-pages/org/topic-tag sample post: https://github.com/lwasser/data-lesson-catalog/edit/gh-pages/_posts/lessons/2015-09-10_dc-R.md

relevant YML from sample post

---
layout: post
catalog-entry-type: lesson
title: Data Carpentry R for Ecology
topic-tag: ["Analysis", "Vizualization"] 
---

Code that is not working:

<h3>Data Activities By Topic Tag</h3>
{% for tag in site.topic-tag %}
{% assign t = tag | first %}

{% for atag in site.data.topic-tags %}
{% if atag.slug == t %}
  <h5><a href="{{ site.baseurl }}/{{ atag.slug }}">{{ atag.name }}</h5>
{% endif %}
{% endfor %} 
      ({{ tag | last | size }}) 
{% endfor %}

Can i sort posts by other tags (not just the tags yml element)?

The output that i'd like is something like: Analysis (2) Visualization (3)

If so, any suggestions as to why the code above doesn't work? I found another post on here asking something similar but the resolution was to use the yaml "tags"element which will not work for my use case. Many thanks, Leah

3

3 Answers

0
votes

You can use categories. They are working just like tags and can be another way to sort posts.

0
votes

Hi to anyone who is struggling with this same thing. This is what i've learned.

Tags and categories are built into the jekyll build. You can thus call site.tags or site.categories without adding anything to your config file. Other custom tags that you create like topic-tags, are not inherently understood by jekyl as variables that can be called at the site level. My work-around -- use a counter to count posts by custom variable. the code looks like this

{% for member in site.data.topic-tags %}
  {% assign counter = 0 %}
  <!-- this code counts the number of posts associated with the member -->
  {% for post in site.posts %}
    {% if post.topic-tag contains member.slug %}
    {% assign counter = counter | plus: 1 %}
    {% endif %}
  {% endfor %}  
  <h5><a href="{{ site.baseurl }}/topic-tag/{{ member.slug }}">{{ member.name }} ({{ counter }})</h5>

{% endfor %}

This works like a charm albeit it is looking through all of the posts to see if it contains the member tag in the yaml.

I hope this helps someone!

0
votes

Here the code I use to display number of post on each categories. You may change site.categories to site.tags to display them by tags.

<h2 class="question">Topics ({{ site.posts | size }} total)</h2>
<ul class="topics">
{% capture tags %}
  {% for tag in site.categories %}
    {{ tag[0] }}
  {% endfor %}
{% endcapture %}
{% assign sortedtags = tags | split:' ' | sort %}
{% for tag in sortedtags %}
  <li class="topic-header"><b>{{ tag }} ({{ site.categories[tag] | size }} topics)</b>
    <ul class='subnavlist'>
  {% for post in site.categories[tag] %}
      <li class='recipe'>
        <a href="/{{ site.github.project_title }}{{ post.url }}">{{ post.title }}</a>
      </li>
  {% endfor %}
    </ul>
  </li>
{% endfor %}
</ul>

Check it on action here.