Craft CMS Asked by Vaughn D. Taylor on February 1, 2021
I have blocks set up for nutritional values: Calories, Total Fat, Carbohydrates, etc. These are set up in NEO blocks.
I can successfully loop through all of the NEO blocks (about 20 rows total) and output the content using this for loop:
{% for block in entry.nutritionBuilder.level(1).all() %}
...
{% endfor %}
Later, I added a line ABOVE this loop to retrieve the content of a single block:
{% set item = entry.nutritionBuilder.type('blockCalories').one() %}
{{ item.nutritionValue }}
This works, in that it will output the value I need to retrieve. However, the for loop below this line now only outputs a single row from nutritionBuilder which is blockCalories row.
Can anyone see why this is happening?
This is because of a change in Craft 3 and you need to clone
your query (docs).
I have not tested this but the below should work:
{% set blocks = entry.nutritionBuilder %}
{% set item = clone(blocks).type('blockCalories').one() %}
{{ item.nutritionValue }}
{% for block in clone(blocks).level(1).all() %}
...
{% endfor %}
Answered by Oli on February 1, 2021
Oli, thanks for pointing me in the right direction. Your answer was close, but there's a slight modification:
{% set blocks = entry.nutritionBuilder %}
{% set item = clone(blocks).type('blockCalories').one() %}
{{ item.nutrionValue }}
{% for block in clone(blocks).level(1).all() %}
...
{% endfor %}
The cloning needs to happen when setting the item
variable as well as in the for loop.
Answered by Vaughn D. Taylor on February 1, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP