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 %}
  • If you view the source on the page, what gets output where you expect the image URL to be? – Brad Bell Feb 22 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. – analog junkie Feb 23 at 8:50
  • I have the exact same problem, when I load an url of a channel asset field. – analog junkie Feb 25 at 11:46
  • Is it possible that it has to do with the fact, that the site runs on mamp? – analog junkie Feb 25 at 11:55
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

|improve this answer|||||
1

Try checking for |length

         {% if image|length %}
          {{ image.url }}
         {% endif %}
|improve this answer|||||
  • Still. Other parameters of the image are printed like id or title but not the url. – analog junkie Feb 23 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 %}
|improve this answer|||||
  • Same result. I get an id of the image and the proper title, but it will not print the url. – analog junkie Feb 23 at 7:58
  • Why do you need {% if block.type == "image" %} ? Have you defined the block.type as 'image'? try removing it. – Steve Feb 23 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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