2

I was using this code in the past and it worked well

        {% set assets = craft.assets.limit(null) %}
        {% set entries = craft.entries.section('menu').limit(3).order('RAND()').relatedTo({
            targetElement: assets,
            field: 'assets'
        }) %}

        {% for item in entries %}
                {% set i = item.assets.first() %}

                <img width="{{ i.getWidth('medium') }}" height="{{ i.getHeight('medium') }}" src="{{ i.getUrl('medium') }}" alt="{{ recipe.title }}" />

        {% endfor %}

However, in the new version the standalone "assets" field became a part of the matrix field so this piece of code doesn't work anymore. Please advise :)

1 Answer 1

1

You can access the assets field by referencing the matrix field first. myMatrixFieldHandle.assets. So your entries code would be something like this.

{% set entries = craft.entries.section('menu').limit(3).order('RAND()').relatedTo({
       targetElement: assets,
       field: 'myMatrixFieldHandle.assets'
}) %}
3
  • Thanks but I've got this error instead. Craft\EntryModel and its behaviors do not have a method or closure named "assets".
    – Mark H.
    Commented Oct 30, 2015 at 1:41
  • 1
    It would be whatever you named your assets field.
    – a-am
    Commented Oct 30, 2015 at 2:32
  • In my case the matrix fieldtype called "contentBuilder" and assets field name is "image. {% set assets = craft.assets.limit(null) %} {% set entries = craft.entries.section('menu').limit(3).order('RAND()').relatedTo({ targetElement: assets, field: 'contentBuilder.image' }) %} {% for item in entries %} {% set i = item.image.first() %} <img src="{{ i.getUrl('medium') }}" /> {% endfor %}
    – Mark H.
    Commented Oct 30, 2015 at 19:40

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.