anta
September 20, 2021, 10:52am
1
Hello,
I’m trying to show every file from a children page files field and shuffle them all.
So far it works, but with my approach I can only show a list of every file from every children page and shuffle them within that children page, rather than sort them within all children files.
Is what I’m trying to do something doable?
Thanks in advance.
Here’s my snippet:
<div class="grid">
<?php foreach ($photographyPage->children()->listed() as $project): ?>
<?php $images = $project->thumbnails()->toFiles()->shuffle();
foreach($images as $image): ?>
<div class="wrapper">
<a href="<?= $image->parent()->url() ?>">
<img class="thumb" src="<?= $image->url() ?>" title="<?= $image->parent()->title() ?>">
</a>
</div>
<?php endforeach ?>
<?php endforeach ?>
</div>
texnixe
September 20, 2021, 11:11am
2
$files = new Files();
$page->children()->map(function($item) use($files) {
$files->add($item->thumbnails()->toFiles());
});
dump($files->shuffle());
Or
$files = new Files();
foreach ($page->children() as $child) {
$files->add($child->thumbnails()->toFiles();
}
dump($files->shuffle());
anta
September 20, 2021, 11:30am
3
Hello,
Thanks for that – excuse my most likely lack of knowledge, but how can I apply your snippet to mine? I don’t seem to find how to apply this logic to it.
Thanks
texnixe
September 20, 2021, 11:36am
4
<?php
$images = new Files();
$page->children()->map(function($item) use($images) {
$images->add($item->thumbnails()->toFiles());
});
$images = $images->shuffle();
?>
<?php if ($images->isNotEmpty()): ?>
<div class="grid">
<?php foreach($images as $image): ?>
<div class="wrapper">
<a href="<?= $image->parent()->url() ?>">
<img class="thumb" src="<?= $image->url() ?>" title="<?= $image->parent()->title() ?>">
</a>
</div>
<?php endforeach ?>
</div>
<?php endif; ?>
anta
September 20, 2021, 11:54am
5
Thanks a lot. That’s perfect.
Any idea why another foreach
loop I’m using for the same page stops working when I add the map
?
texnixe
September 20, 2021, 11:57am
6
I lost my crystal ball, could you provide your code?
anta
September 20, 2021, 1:24pm
7
Yep. Sorry.
I have this snippet after the one you provided.
<!-- Filter -->
<div class="filter">
<ul class="projects-list">
<?php foreach ($photographyPage->children()->listed() as $project): ?>
<li id="project-<?= $project->indexOf() ?>" class="<?php foreach ($project->tags()->split() as $tag): ?><?= (str_replace(' ', '-', strtolower($tag))) ?> <?php endforeach ?>"><a href="<?= $project->url() ?>"><?= $project->title() ?></a></li>
<?php endforeach ?>
</ul>
<ul class="projects-tags">
<?php $tags = $photographyPage->children()->listed()->pluck('tags', ',', true);
foreach ($tags as $tag): ?>
<li class="button" id="<?= str_replace(' ', '-', strtolower($tag)) ?>">
<?= $tag ?>
<?php $count = $photographyPage->children()->listed()->filterBy('tags', $tag, ',')->count(); ?>
<span>(<?= $count ?>)</span>
</li>
<?php endforeach ?>
</ul>
</div>
<!-- end of filter -->
It stops working after I include this in the code:
<?php
$images = new Files();
$page->children()->map(function($item) use($images) {
$images->add($item->thumbnails()->toFiles());
});
$images = $images->shuffle();
?>
texnixe
September 20, 2021, 1:35pm
8
Have you replace this with $photographyPage
instead of $page
?
If that’s not the issue, I think I need the complete template code to see where it’s going wrong.
1 Like