# Counting files from structure-field?

**URL:** <https://forum.getkirby.com/t/counting-files-from-structure-field/16832>\
**Category:** Questions\
**Tags:** v3\
**Created:** [January 10, 2020, 7:09am UTC](https://forum.getkirby.com/t/counting-files-from-structure-field/16832 "2020-01-10T07:09:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoSch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/josch/32/10191_2.png) [@JoSch](https://forum.getkirby.com/u/JoSch)\
**Post date:** [January 10, 2020, 7:09am UTC](https://forum.getkirby.com/t/counting-files-from-structure-field/16832/1 "2020-01-10T07:09:09Z")

</div>

I’m having trouble counting and displaying the amount of images in a files-field inside a structure-field.

I’m trying the following:

```
<?php $blogPosts = $page->blogPosts()->toStructure();
	  foreach($blogPosts->limit(4) as $blogPost): 

      $postImages = $blogPost->images();
	  $imageCount = $postImages->count(); ?>

	  <div id="myElement" data-images="<?= $imageCount ?>">
	     <!-- ... -->
	  </div>

<?php endforeach ?>

```

But I keep getting this exception error:

 ![Screen Shot 2020-01-10 at 08.06.59](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/7/7e8ff7b4c4ce792a48601906bc613f929d06f184.png)

I have counted files using `count()` many times before. Why is it not working here? Does the field behave differently because it is part of a structure-field?

---

<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:** [January 10, 2020, 8:05am UTC](https://forum.getkirby.com/t/counting-files-from-structure-field/16832/2 "2020-01-10T08:05:08Z")

</div>

No, it doesn’t behave differently. I assume you field is called images? You have to convert the content of that field to a files object, no matter if that field is inside a structure field or not.

```auto
$postImages = $blogPost->images()->toFiles();
$imageCount = $postImages->count(); ?>

```

The reason why your code works outside of a structure field when the field is called images, is that `images()` is a page method that gets all images from a page. The field name images in this cases conflicts with the method name.
