I am not sure why, but I cannot seem to get the short name/handle/slug of a matrix field that is a category field. I need to get it's slug.

{% for row in entry.matrixField %}

 {{ row.matrixChild.slug }}

{% endfor %}
  • Just a side note: looping through Queries is deprecated. You should take a look into your deprecator logs – Robin Schambach May 11 at 22:29
up vote 2 down vote accepted

That is returning an ElementQuery. I'm assuming you only need to get the first category. You need to use .one to get the category element.

{% for row in entry.matrixField.all() %}
    {{ row.matrixChild.one.slug }}
{% endfor %}

If you wish to display more than one category you can use a {% nav %} or {% for %} to loop through them. See the category docs.

  • Thanks Simon! I had a few of those fields empty and I got an error so I added {% if row.matrixChild.one|length %} around it and it works :) – Vin May 11 at 22:44
  • You're welcome. Adding |length is exactly what I would do too. – Simon Kuran May 11 at 22:46

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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