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?

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
KSPR
  • 3,786
  • 2
  • 29
  • 52

1 Answers1

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 %}
Brad Bell
  • 67,440
  • 6
  • 73
  • 143