Image Caption Wont Show Up

Me again… :hand_splayed:

I want to add an image caption. I structured my blueprint like this:

files:
	fields:
		caption:
			label: Caption
			type: textarea

And tried to call it with:

<figcaption><?php echo $file->caption()->html() ?></figcaption>

But it will give me an error. Any ideas?

What sort of error do you get?

Could you please post the context, not just that single line? Might be that the object does not exist…

I will get this error:

This page is currently offline due to an unexpected error. We are very sorry for the inconvenience and will fix it as soon as possible.

I want to have it here:

<div class="swiper-wrapper">
	<?php foreach($page->gallery()->yaml() as $image): ?>
		<?php if($img = $page->image($image)): ?>
			<div class="swiper-slide">
				<figcaption><?php echo $file->caption()->html() ?></figcaption>
				<img src="<?php echo $img->resize(1440, null, 80)->url() ?>" alt="<?= $page->title()->html() ?>"  class="swiper-lazy" />
				<div class="preloader"></div>
            </div>
		<?php endif ?>
	<?php endforeach ?>
</div>

What is the name of the image field in the structure field? You should have something like:

<?php if($img = $page->image($image['name-of-field'])): ?>

(Or use the toStructure() method instead to be able to use something like $image->name_of_field()). With a structure field you always have to specify the individual fields.

Please turn on debugging in your config.php

c::set('debug', true);

to get a useful error message

debuggin says:

Undefined variable: file

Oh, right, in addition to what I wrote above, you use $img as variable, not $file.

1 Like

:rolling_eyes: so easy…

Thank you very much!