Specific entry in a structure field with Kirby 2.4

Hey there,

I know that my issue was already solved in this post here. And I was already using this solution in my project to create an openening image for a lightbox. The problem is, that the last project this solution worked was still with Kirby 2.3.2 and I already started the new project with Kirby 2.4 and the solution isn’t working anymore.

The thing I’m curious about is, I get the whole (specific) entry but not one specific field within the entry, like:

this works:

$lbopen = $page->lightbox()->toStructure()->nth(0);
echo $lbopen;

this not:

$lbopen = $page->lightbox()->toStructure()->nth(0);
echo $lbopen->image();

It would be great if there is any solution, because in the near future I want to update the last project were the code is working.

Thanks upfront and cheers

I think the issue is doing it with echo. Does this work?

$lbopen = $page->lightbox()->toStructure()->nth(0);
$myimage = $lbopen->image();
echo $myimage;

I tried to reproduce your issue in a fresh Starterkit but couldn’t. Do you get an error message? Or what is the result?

@jimbobrjames: Echoing the field without storing the value in a variable first is not the issue here.

Thanks for the fast reply,
and oh my god am I stupid, I haven’t really checked the subpage. I used the used the code in a loop and thats were it breaks.

Here’s the full code:

<section class="lightbox clearfix">
<?php foreach($page->children()->visible() as $lightbox): ?>

<article class="lb clearfix">
<?php 
$lbopen = $lightbox->lightbox()->toStructure()->nth(0);
echo $lbopen->lbimage();
?>

<?php echo $lightbox->mainimage()->toFile() ?>
</article>

<?php endforeach ?>
</section>

I’m sorry for the confusion :sweat:

Does it work now or do you still get errors?

It works as long as it’s not in a loop.
To specify my problem, I want to create a page with mulitiple lightboxgalleries. So I create a page called ‘gallery’ as an overview and create a childpage for each gallery to store the content. In a childpage I have one mainimage to trigger the gallery and a structured field for the lightboximages. That means the mainimage has to link to the first image in the structure.

The childpages work with that code:

<?php $lbopen = $page->lightbox()->toStructure()->nth(0) ?>
<a href="<?php echo $page->url() . '/' . $lbopen->lbimage() ?>" class="thumb">
<?php echo $page->mainimage()->toFile() ?>
</a>

But I get the error as soon I get to the overviewpage where I have to put it in a loop to I fetch all childpages.

So this doesn’t work:

<?php foreach($page->children()->visible() as $lightbox): ?>
<article class="lb clearfix">
<?php $lbopen = $lightbox->lightbox()->toStructure()->nth(0) ?>
<a href="<?php echo $lightbox->url() . '/' . $lbopen->lbimage() ?>" class="thumb">
<?php echo $lightbox->mainimage()->toFile() ?>
</a>
</article>
<?php endforeach ?>

The error I get is:
‘Call to a member function lbimage() on boolean’

Thanks again

Have you made sure that each of the subpages contains a structure field?

You should always check if an object exists (in this case, $lbopen) before calling a method (lbimage()) on it.

Oh my god! I’m such an idiot! :scream:

Thanks now it works, I had one page where there was no content in the structure.
Thanks alot, even for most obvious error I had there. Really really thanks for that

At least now I know that the forumsupport is awesome, keep up the good work.
I’ll mark this topic as closed and go to my corner of shame :smile:

cheers
tom

1 Like