Support

Account

Home Forums Add-ons Gallery Field ordering images of a gallery according to datepicker inside attachment page Reply To: ordering images of a gallery according to datepicker inside attachment page

  • 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; ?>