Can you list content from subpages of another page?
E.g. the following site structure:
Home
Products
Product
Product
Product
Would it be possible with Kirby to retrieve the pictures of the different products from the “Products” subpages in the “Home” template and display them on the “Home” page? What PHP code would you need?
What should the code look like for a particular image called “productpic”? So if you want to display just the specific “productpic”-images from the “product”-subpages (one per page) on the homepage instead of all the images?
You really shouldn’t use the syntax above. If you want to use curly braces, don’t close your php tags and open them on a new line again. If you want to start with a new tag, use the following syntax (with colons and endif/endforeach)
But actually, if you don’t use any HTML tags in between, your code would be better off like this:
<?php
if ($page = page('products')) {
// this is the beginning of a variable definition using a filter with callback
$images = page('products')->children()->images()->filter(function($image) {
return $image->parent()->productpic()->toFile() === $image;
}); // it ends here and shouldn't be interrupted with new PHP tags
foreach ($images as $image) {
echo $image;
}
}
?>
And certainly don’t start a new php tag in the middle of a closure.
<?= is a short echo tag, that is the same as <?php echo and is actually preferable to the long version.
However, all texts inserted from the homepage “home.txt” that follow this code are no longer displayed. All texts prior to it are. I didn’t notice this at first, because I didn’t scroll down.
Any idea why that might be?