1

First time working with matrix fields. In this case to load images. But somehow I can't manage to fetch the url of the Images. I can get the title and id but not the url. Can someone point me to the mistake am am making? Thanks!

{% set allProjects = craft.entries.section(['sectionHandle']) %} 

    {% for projects in allProjects.all() %} 

        {% for block in projects.matrixFieldHandle.all() %}

           {% if block.type == "image" %}

             {% set image = block.image.one() %}
             {% if image %}
              {{ image.url }}
             {% endif %}

           {% endif %}

       {% endfor %}

    {% endfor %}
4
  • If you view the source on the page, what gets output where you expect the image URL to be?
    – Brad Bell
    Commented Feb 22, 2020 at 5:55
  • I can print {{ image.id }} ... title, width, height, but if I ask for {{ image.url }} there is nothing printed on screen or in the source. It's empty. Commented Feb 23, 2020 at 8:50
  • I have the exact same problem, when I load an url of a channel asset field. Commented Feb 25, 2020 at 11:46
  • Is it possible that it has to do with the fact, that the site runs on mamp? Commented Feb 25, 2020 at 11:55

3 Answers 3

2

during this process : {% set image = block.image.one() %} please make sure the name of handle of image is image. For example in this matrix the type is 'image' but to get image url you should call it. item.imageAssets.one().url Matrix

1

Try checking for |length

         {% if image|length %}
          {{ image.url }}
         {% endif %}
1
  • Still. Other parameters of the image are printed like id or title but not the url. Commented Feb 23, 2020 at 7:46
1

block.image is actually an array (even if only 1 image in it) therefore you need to loop over it so try this...

{% for image in block.image.all() %}{{ image.url }}{% endfor %}

If you want to check that there is an image first you can use this..

{% if block.image|length %} ... {% endif %}
2
  • Same result. I get an id of the image and the proper title, but it will not print the url. Commented Feb 23, 2020 at 7:58
  • Why do you need {% if block.type == "image" %} ? Have you defined the block.type as 'image'? try removing it.
    – Steve
    Commented Feb 23, 2020 at 9:43

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.