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.
1 Answer
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?
-
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? Aug 19, 2016 at 4:13