2

How can I get the fields of a matrix field? I want to loop through the fields. I have the fields of the section, if the field.handle is of type matrix, I want to get the fields of the matrix.

4

Looping through a Matrix field is pretty easy, here is an example. Also read the docs on the Matrix field here - https://craftcms.com/docs/matrix-fields

{% for block in entry.myMatrix %}
    {% set handle = block.type.handle %} {# Grab the current block handle #}

    {{ handle }} {# Echo out the handle #}

    {% if handle == 'myMatrixBlock' %}
        {{ myMatrixBlock.heading }}  {# If you have a field called heading in myMatrixBlock #}
    {% endif %}

{% endfor %}

Updated

Take a look at these examples...

getting fields of a section (getFields())

How to loop through matrix block fields and pull out handle and content?

| improve this answer | |
  • I know how to get it when I know the fields. I want to get the fields programatically. In the example you give, I'd like to get heading programatically. Do you know how to do it? – user2997606 Aug 19 '16 at 4:13

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.