1

I want to get all images from the section projekte which are bound to the asset field bild I then want to fiddle around with .limit() and maybe random filters.

So I grabbed all images like described here

This works but as soon as I add .limit()

{% set projectImg = craft.entries.section('projekte').find() %}
{% set projectImages = [] %}

{% for item in projectImg %}
  {% set currentImage = item.bild %}
  {% set projectImages = projectImages | merge(currentImage) %}
{% endfor %}

{% for asset in projectImages.limit(3) %}
   <img src="{{ asset.url }}">
{% endfor %}

This results in

PHP Notice: Array to string conversion

Why can't I just ad whatever filter I want on that new loop now?

1 Answer 1

0

This is a misunderstanding of the difference between an ElementCriteria model and actual fetched elements.

Some useful background reading:

https://craftcms.com/docs/templating/elementcriteriamodel

What are the differences between an elementCriteriaModel and fetched elements?

You could do what you're looking for using Twig's random method:

{% set projectImg = craft.entries.section('projekte').find() %}
{% set projectImages = [] %}

{% for item in projectImg %}
    {% set currentImage = item.bild %}
    {% set projectImages = projectImages | merge(currentImage) %}
{% endfor %}

{# Grab 3 random images from the array #}
{% for counter in 0..2 %}
    {% set asset = random(projectImages) %}

    <img src="{{ asset.url }}">
{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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