Timber Twig Get post_tag with out joining
Asked Answered
F

0

6

I'm trying to output my post_tags for a post and using the example from the Timber docs. It works as expected but I'm trying to show each tag as a button. I'm sure it's probably seemingly simple. I just cannot figure it out.

Right now both tags show as one button. I've tried the for loop instead of the if statement, it works visually (each tag as button) but the for loop seems to pull all of the post_tags; even the ones not being used for this particular post. I tried 'hide_empty' but that didnt work either. Is there any other way to go about this? thank you!!

PHP file

$context['categories'] = Timber::get_terms('post_tag');

Twig File with just the if statement (outputs the 2 tags that are on this post but is one button)

{% if categories %}
      <a href="{{post.term.link}}" class="btn btn-primary">{{ post.terms('post_tag') | join(', ') }}</a>
      <pre>{{post.terms('post_tag')|print_r}}</pre>
 {% endif %}

Twig file with the for loop (outputs all of the post_tags with each tag as a button)

 {% for term in categories %}
            <a href="{{post.term.link}}" class="btn btn-primary">{{term.name}}</a>   
        {% endfor %}

Is there a way to show them separately?

SOLVED:

Yikes. I was looking in the wrong area. Should be checking the post instead of the term.

 {% for term in post.terms('tags') %}
    <li><a href="{{term.link}}">{{term.name}}</a></li>
{% endfor %}
Fieldwork answered 15/3, 2017 at 2:49 Comment(1)
For tags in particular, you can also use post.tags(). See docs.Merrimerriam

© 2022 - 2024 — McMap. All rights reserved.