Jump to content

Sort images by title in image Inputfield


Harmster
 Share

Recommended Posts

Hey,

I've made a gallery and it all works fine, all the images are in an image inputfield and when I upload a bunch it puts the images in the order the pictures are uploaded.

How can I sort the pictures on the title?

The titles are like following mih_5500-1200.jpg and mig_5555-1200.jpg etc etc

I display the pictures with

<?php
foreach($page->images as $image){
//blabla
}
?>
Link to comment
Share on other sites

Untested, but I think this should do the trick:

$page->images->sort("title");

Then you can foreach through them.

Although you might have some issues with the numbers in the filenames, in which case you might need to make use of PHP's natsort:

$images = $page->images->getArray();
natsort($images);

$reversed_images = array_reverse($images);

$sortedImages = new WireArray();
$sortedImages->import($reversed_images);

$page->images = $sortedImages;
Link to comment
Share on other sites

Actually, I believe it should be 'name' rather than 'title:

$images = $page->images->sort("name");

//OR
$images = $page->images->find('sort=name');

//OR, by the way, sort descending by name
$images = $page->images->find('sort=-name');

//OR, by the way, sort descending by name
$images = $page->images->sort("-name");

Tested  :)

Btw, you can also sort by other properties, including description, height, width, size (see to be different from "filesize"?), etc. See a list here

Edited by kongondo
  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...