Sort Filenames in Ascending order

Hi there,

I’m trying to pull all the image-filenames (without the extension, (‘jpg’) and the /path.); from my page children.

<?php foreach($page->children() as $a): ?>
<?php echo $a->files() ?>
<?php endforeach ?>

however I’m struggling to get the Ascending sortBy function to work.

<?php foreach($page->children() as $a): ?>
<?php echo $a->files()->sortBy('sort', 'asc') ?>
<?php endforeach ?>

Any ideas what I’m doing wrong?
Appreciate the guidance,

What do you want to sort, all files from all children, or just the files from each single page? Currently, you are trying to sort the images of each page individually. Also, you are using the manual sort order (sort field in image meta file).

Hi texnixe,

I want to sort and display all filenames from all the children pages within a page.
Structure looks like this:

   — Parent
       — Subpage1
            — File1
       — Subpage2
            — File2
       — Subpage3
            — File3

What is the best way of sorting images?


Option 1: individually by page

<?php foreach($page->children() as $a): ?>
    $images = $a->images()->sortBy('name', 'asc');
    foreach($images as $image):
      echo $image->name();
<?php endforeach ?>

Option 2: all images from all children:

$images = $page->children()->images()->sortBy('name', 'asc');
foreach($images as $image) {
  echo $image->name() . '<br>';


works lovely, and make total sense.
appreciate the help always.


1 Like