Getting Full path of image field from children

I am trying to show an image from each child on the parent page:

<?php foreach($page->children() as $subpage): ?>
  <img src="<?= $subpage->thumb()->url();?>"/>
<?php endforeach ?>

I am getting the image file name, but without the path to the folder it is stored in e.g http://mysite.com/myimage.jpg.

That’s because you’re getting the value of the thumb field, ie. the filename, but you’re not accessing the file object itself before calling the url method.

 <img src="<?= $subpage->thumb()->toFile()->url(); ?>"/>

should solve your issue.

Additionally, I would recommend to check if the file exists:

<?php if ($image = $subpage->thumb()->toFile()): ?>
  <img src="<?= $image->url(); ?>"/>
<?php endif; ?>

thanks - but i get: Call to a member function url() on null

thanks! this is working great.

1 Like