I'm pulling my hair out trying to create a custom page--custom.html.twig for a content type and getting taxonomy terms to display.

I'm currently using the following piece of code:

{% if node.field_work__services.0 %}
      {% for key, item in node.field_work__services %}
         <li>{{ item.title }}</li>
      {% endfor %}
{% endif %}

The if statement resolves, so I get a UL with an equivalent number of LIs to the number of taxonomy terms for that node... however, I can't get it to output the actual taxonomy term.

Anyone have any idea how to do this?

  • As an FYI, I have tried item.value and many others to no avail. Even if i break it out of the conditional and just use node.field_work__services.0.value (or any other key I've tried).. no dice. – L. Rohr Dec 27 '18 at 15:51
  • is page--custom.html.twig custom template for page, or node ?? – berramou Dec 27 '18 at 16:30

Try with:

{% if node.field_work__services|length %}
        {% for item in node.field_work__services %}
          <li>{{ item.entity.label }}</li>
        {% endfor %}
{% endif %}
| improve this answer | |
  • And there you have it! That works wonderfully. Thank you so much! As a bonus, would you happen to know how to get the URL for that as well? I tried item.entity.url_callback and item.entity.links.canonical to no avail.... – L. Rohr Dec 27 '18 at 18:50
  • Answered my own question here: drupal.org/forum/support/post-installation/2016-04-19/… – L. Rohr Dec 27 '18 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.