Hi,
I am trying to pulling a single image from each subpages to homepage. I have six subpages I can pulling the title but not an image from each subpages. Can please tell me what I am doing wrong.
<?php foreach ($page->children()->listed() as $product): ?>
In this first line, you assign each child page in the loop to the $product variable.
But then in your third line
<?php if ($product = $page->cover()->toFile()): ?>
You assign a file from $pageto the same $product variable. What you would have to do, is get the file from $product–the child page– and use another variable for the image.
<?php foreach ($page->children()->listed() as $product): ?>
<div class="col-lg-4">
<!-- we try to get a file from the `$product` child and assign it to the `$image` variable -->
<?php if ($image = $product->cover()->toFile()): ?>
<a href="<?= $product->url() ?>">
<figure class="product-image">
<img src="<?= $image->url() ?>" alt="">
</figure>
<h3 class="product-title"><?= $product->title() ?></h3>
</a>
<?php endif ?>
</div>
<?php endforeach ?>
Texnixe, thank you for your time to help me understand what is going on. I understand a little better, that I have to assign a new variable to images in order to pull images from the child pages. Maybe I am missing something here but it’s repeating images.
Well, but according to what you outlined above, it seems you are pulling in the children of the outdoor page? Because the headlines under the image say “Dining Table”, “Chaiselounge” etc. And apart from that, I can’t see a field called cover in that text file?
So maybe you can explain again in more detail what exactly you are trying to achieve and on what page.