Problem with image in structure field: can't find absolute url image

Ok, so I have a site where I want to get the image field working.

This is my code:

<?php foreach($page->lightbox()->toStructure() as $lightbox): ?>
	<a href="<?php echo $lightbox->lightbox_image()->toFile()->url() ?>" data-rel="lightcase:<?php echo $lightbox->lightbox_collection() ?>" title="<?php echo $lightbox->lightbox_caption() ?>"></a>
	
	<a href="<?php echo $lightbox->image($lightbox->lightbox_image())->url() ?>" data-rel="lightcase:<?php echo $lightbox->lightbox_collection(); ?>" title="<?php echo $lightbox->lightbox_caption(); ?>"></a>
	
	<?php endforeach ?>	

both statements won’t work.
not sure why.
amI doing something wrong?

You want to find the image of a page so you’ll have to do it like that:

<?php echo $page->image($lightbox->lightbox_image())->url() ?>

or a bit shorter like that:

<?php echo $lightbox->lightbox_image()->toFile()->url() ?>

And make sure to check if the image exists, before calling the URL method:

<?php foreach($page->lightbox()->toStructure() as $lightbox): ?>
  <?php if($image = $lightbox->lightbox_image()->toFile()): ?>
    <a href="<?php echo $image->url() ?>" data-rel="lightcase:<?php echo $lightbox->lightbox_collection(); ?>" title="<?php echo $lightbox->lightbox_caption(); ?>"></a>
  <?php endif ?>
<?php endforeach ?>	

both won’t work…

This is my blueprint:
indent preformatted text by 4 spaces

  lightbox:
    label: Lightbox
    type: structure
    entry: >
      {{lightbox_image}}<br />
      {{lightbox_caption}}<br /> 
      {{lightbox_collection}}
    fields:
      lightbox_image:
        label: Image 
        type: select
        options: files
      lightbox_caption:
        label: Image Caption
        type: textarea
      lightbox_collection:
        label: Collection
        type: text

This code works:

<?php foreach($page->lightbox()->toStructure() as $lightbox):  >
  <a href="" data-rel="lightcase:<?php echo $lightbox->lightbox_collection() >" title="<?php echo $lightbox->lightbox_caption() ?>"></a>
<?php endforeach >	

But I’m just struggling to get the href="" filled.

The above code should be correct. What do you get in your href attribute?

See this cookbook recipe for reference: https://getkirby.com/docs/cookbook/handling-images-in-kirby#accessing-images-in-templates

Edit: BTW, in your last post, some PHP opening tags are not correct (question mark missing).

yes i know, but this was because the code displayed wrongly in the forum…
when i fill in your code, there is no output although I’m sure there are images there in the panel, in the structurefield

Have you checked the source code? Since there is nothing between the opening and the closing a tag, nothing will be visible on the page… just asking :innocent:

Please wrap blocks of code within three backticks on a separate line before and after the block.

i think i found the problem. It was indeed something with the images on the server.
Thanks for helping me!

Enjoy: