# Display subset image with if funtion

**URL:** <https://forum.getkirby.com/t/display-subset-image-with-if-funtion/4927>\
**Category:** Questions\
**Tags:** v2\
**Created:** [August 9, 2016, 11:32pm UTC](https://forum.getkirby.com/t/display-subset-image-with-if-funtion/4927 "2016-08-09T23:32:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![graphicmes](https://avatars.discourse-cdn.com/v4/letter/g/ee7513/32.png) [@graphicmes](https://forum.getkirby.com/u/graphicmes)\
**Post date:** [August 9, 2016, 11:32pm UTC](https://forum.getkirby.com/t/display-subset-image-with-if-funtion/4927/1 "2016-08-09T23:32:51Z")

</div>

Hello Everyone.  
I have a folder structure set up as:  
0101/em.jpg  
0102/hp.jpg  
0102/em.jpg  
0103/em.jpg

I am trying to display a section with just hp images and another with just em images. Currently I have this working (with the help of this forum)

```auto
<?php foreach ($page->children() as $child): ?>
<div><?php echo $child->uid() ?></div>
<div>   
<?php foreach($child->images()->filterBy('filename', '*=', 'HP') as $image): ?>
 <a href="<? echo $image->url() ?>"><img src="<? echo $image->url() ?>" alt="<? echo $image->title() ?>" /></a>
<?php endforeach ?>
 </div>
<?php endforeach ?>

```

This results in lots of empty sections and one hp image displaying. How can I have it display only the child folder with a hp image? if statement? hasImages()?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [August 10, 2016, 12:10am UTC](https://forum.getkirby.com/t/display-subset-image-with-if-funtion/4927/2 "2016-08-10T00:10:01Z")

</div>

If I understand correctly, then try this:

```auto
<?php 
// fetch children with hp images only
$hpChildren = $page->children()->filter(function($child) {
  return $child->images()->filterBy('filename', '*=', 'HP')->count() > 0;
});

foreach ($hpChildren as $child): ?>
  <div><?php echo $child->uid() ?></div>
  <div> 
    <?php 
    $images = $child->images()->filterBy('filename', '*=', 'HP');
    foreach($images as $image): ?>
     <a href="<? echo $image->url() ?>"><img src="<? echo $image->url() ?>" alt="<? echo $image->title() ?>" /></a>
    <?php endforeach ?>
  </div>
<?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:57pm UTC](https://forum.getkirby.com/t/display-subset-image-with-if-funtion/4927/3 "2025-02-06T16:57:20Z")

</div>


