1

I am creating an archive by date and using this solution.

I want to add the category of the post next to the post and am having trouble capturing the category name of a post since categories are indexed through the site

    {% else %}
    <li><a href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a> 
    {% capture category_names %}{{ ## need to capture category }}{% endcapture %}
    {% if category_name %} - {% for category in category_names %}
              <a href="{{site.baseurl}}/categories/#{{category|slugize}}">{{category}}</a>
              {% unless forloop.last %}&nbsp;{% endunless %}
              {% endfor %}
    {% endif %}
    {% if post.date %} - {{ post.date | date: "%m/%d/%Y" }}{% endif %}</li>
{% endif %}

Currently the code above creates {post name } - - mm/dd/YYYY as I want without the category name in between the 2 '-'.

Any idea how to capture the category name of a post? Thank you.

Patrick
  • 292
  • 1
  • 11
  • 1
    Depending on your post file front matter, try either `post.category` or `post.categories`. You also can't run the result of a `capture` through a `for` loop - the result is always a string. – Ross Jul 19 '21 at 22:01
  • That worked, thank you! my front matter is categories but I was using category in my code causing the error. – Patrick Jul 26 '21 at 16:21

0 Answers0