How do I modify a template to include a loop that displays all custom fields that I added to the field group that the current entry belongs through? I know how to display the fields individually by calling their name, but how to display all fields belonging to the group?
1 Answer
Field groups are not specific to a particular channel or channel entry. They are just an arbitrary organization within the control panel for ease of use. What you would be looking for are the fields defined for the particular field layout for that entry. I.e.:
{# loop through the fields in the entries fieldLayout Model #}
{% for fieldLayoutField in entry.getFieldLayout().getFields() %}
{# get the field Model from the fieldId #}
{% set field = craft.fields.getFieldById(fieldLayoutField.fieldId) %}
{# print the field handle and the field content #}
{{ field.handle }} : {{ entry.getContent()[field.handle] }}
{% endfor %}
By the way, there is an excellent plugin called 'Inspector' written by Adrian McNeal that displays all methods/attributes/properties of an object or variable, which can be very useful for figuring out what your dealing with.
UPDATE: It has been brought to my attention thatfieldLayoutField.fieldId
has been discontinued in favor of fieldLayoutField.id
. While not tested, if the above does not work, please try the following instead:
{% set field = craft.fields.getFieldById(fieldLayoutField.id)
-
This code has already proven useful, so thanks a lot. I wonder if it could be modified to go a level deeper: what I'm trying to get at is the fields within Entry fields when those entry fields are part of a Single. As in, if you have a Photos field, get it to show photos.caption, things a level down. Jul 5, 2017 at 3:12
-
@DavidRhoden Each field type is quite different, so you would probably have to add some switch conditionals to access specific field properties. But doable. Might be better to ask a new question if you are looking for something specific. Jul 11, 2017 at 3:46