Collection of images

Hey Everyone, I’m racking my brain trying to figure out how I can display a collection of images across all folders on one page.

I want to run a search through all folders and display only the images where a match occurs.
EX.
Folder 1 - Image A
Folder 2 - Image B
Folder 3 - Image A

I want a page to display only folders with image A. Thanks in advance for all your help. Still learning and love to see what you all can do.

You might find $pages->index() handy:

$all_pages = $pages->index();

foreach ($all_pages as $page) {
  if ($page->images('image_a.jpg')) {
   $src = $page->images('image_a.jpg')->url();
   echo '<img src="' . $src '" />';
 }
}

untested but probably

Should be $page->image('some-image') instead of $page->images('some-image'):

<?php
$all_pages = $pages->index();
foreach ($all_pages as $page): ?>
 <?php if ($image = $page->image('image_a.jpg')): ?>
   <img src="<?php echo $image->url() ?>" >
 <?php endif ?>
<?php endforeach ?>

(And avoid echoing html tags …)

@texnixe So I tried using what you gave me, but the page stops after the page uid. Thoughts?

<?php snippet('header') ?>
<div class="container">
    <div class="twelve columns section">
        <?php echo ($page->uid()) ?>
    </div>
    
        <?php
            $all_pages = $pages->index();
            foreach ($all_pages as $pa): ?>
                <?php if ($image = $pa->image()->filterBy('filename', '*=', 'HP')): ?>
                    <img src="<?php echo $image->url() ?>" >
                <?php endif ?>
        <?php endforeach ?>
</div>
<?php snippet('footer') ?>

You are using the filterBy() method on a single image:

<?php if ($image = $pa->image()->filterBy('filename', '*=', 'HP')): ?>

This will not work, because you can only filter a collection, so change this to

<?php if ($image = $pa->images()->filterBy('filename', '*=', 'HP')): ?>

And turn on debugging in your config.php to debug php errors:

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

Thank you. I’m learning so much. So this fixed my crashing problem, yet nothing comes up. It’s weird because using the same filter command on another page pulls in the correct image.

There’s another problem in your code: Using a filter returns a $files collection, so you would need to use another foreach loop within the first one to output all members of the collection.

<?php
$all_pages = $pages->index();
foreach ($all_pages as $pa): ?>
 <?php if ($images = $pa->image()->filterBy('filename', '*=', 'HP')): ?>
    <?php foreach ($images as $image): ?>
      <img src="<?php echo $image->url() ?>" >
    <?php endforeach ?>
 <?php endif ?>
<?php endforeach ?>