Home › Forums › Add-ons › Gallery Field › ordering images of a gallery according to datepicker inside attachment page
Hello,
i have an option page where i have a gallery field.
I have added a custom field on attachement > a date picker.
I want to sort the images of the gallery according to the date added in the date picker field to create a sort of archive liste like:
december 2017
img 1 ; img 5 ; img 3
january 2018
img 4 ; img 12 ; img 15
…
since i can’t quering this, i’m wondering if there is any chance to do that, or i need to resign and do a repeater field wich is more complex for updating.
—- Versions —-
ACF v5.6.7
WP v4.9.1
i found a part of the code.
The probleme is i store the year in a variable and when the loop see an other year, it’s erase the year to an other one.
So i get many time the same year and the systeme dont put all the images who have the same year in the same header.
here is my code
<?php
$post_id = "option"; // options page
$gallery = get_field('galerie_archive', $post_id);
// vars
$order = array();
// populate order
foreach( $gallery as $i => $image ) {
$order[ $i ] = $image['id'];
}
// multisort
array_multisort( $order, SORT_DESC, $gallery );
// loop through gallery
if( $gallery ):
$current_header = ''; ?>
<?php foreach( $gallery as $i => $image ):
$date = get_field('date', $image['ID'], false);
$temp_header = date_i18n('Y', strtotime($date));
if ( $temp_header != $current_header ) {
$current_header = $temp_header;
echo "<h3>".$current_header.'</h3>';
}
?>
<div>
<img src="<?php echo $image['sizes']['medium']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php the_field('date', $image['ID']); ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
The code you have sorts the field according to their ID value, I’m going to be completely honest, I have never had much luck using array_multisort() and when I need to sort things differently than ACF sorts them then I will generally do sorting in another way.
Anyway, the first thing you need to do is to get the date to sort by instead of sorting by the ID
foreach( $gallery as $i => $image ) {
$order[ $i ] = get_field('date', $image['id'], false);
}
the above code gets the date field attached to the image. The 3rd parameter is false to get the unformatted value of the field to sort by.
Hi John,
thanks a lot for you help !
i share the last version code for the community.
To order images from a gallery according to a date picker field in the attachment :
<?php
// replace by your gallery field name
$gallery = get_field('field_name');
// vars
$order = array();
// populate order
// the above code gets the date field attached to the image.
// The 3rd parameter is false to get the unformatted value of the field to sort by.
foreach( $gallery as $i => $image ) {
$order[ $i ] = get_field('date', $image['id'], false);
}
// multisort
array_multisort( $order, SORT_DESC, $gallery );
// loop through gallery
if( $gallery ):
// variable
$current_header = ''; ?>
<?php foreach( $gallery as $i => $image ):
$date = get_field('date', $image['ID'], false);
$temp_header = date_i18n('Y', strtotime($date));
// generate the year as header
if ( $temp_header != $current_header ) {
$current_header = $temp_header;
echo "<h3>".$current_header.'</h3>';
}
?>
// add the image and the date
<div>
<img src="<?php echo $image['sizes']['medium']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php the_field('date', $image['ID']); ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
The topic ‘ordering images of a gallery according to datepicker inside attachment page’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.