Home Forums ACF PRO Sorting gallery images inside a repeater


Sorting gallery images inside a repeater

  • Hello!

    Some years ago I used code from this thread:

    and modified it into a way I needed it, which is a bunch of galleries on tabs:

    <div class="tabs">
    		<?php if( have_rows('galleries') ):
    		$i = 0;?>
    					<div class="row m-2">
    					<div class="col-md-12 content-area text-center">
    					<ul class="gallery-menu tab-links">
    					<?php while ( have_rows('galleries') ) : the_row(); ?>
    						$gallery_title_name = "repeater_gallery_title";
    						$gallery_title = get_sub_field_object($gallery_title_name); ?>
    						<li><a href="#tab-<?php echo str_replace(' ', '', get_sub_field('repeater_gallery_title')); ?>"><?php the_sub_field('repeater_gallery_title'); ?></a></li>
    					<?php endwhile; ?>
    					<div class="tab-content">
    					<?php while ( have_rows('galleries') ) : the_row(); $i++; ?>
    					<?php $images = get_sub_field('repeater_gallery');
    					if( $images ): ?>
    					<div id="tab-<?php echo str_replace(' ', '', get_sub_field('repeater_gallery_title')); ?>" class="tab <?php 
    					  if ($i == 1) {
    					<div class="row m-2">
    					<div class="col-md-12 content-area text-center">
    						<h3><?php the_sub_field('repeater_gallery_title'); ?></h3>
    					<div class="row d-flex justify-content-center mt-2 mb-4 gallery">
    					<?php foreach( $images as $image ): ?>
    					<div class="col-md-3 text-center gallery-item">
    					<a href="<?php echo $image['url']; ?>" data-lightbox-gallery="<?php the_sub_field('galleries'); ?>">
    					<img src="<?php echo $image['sizes']['thumbnail']; ?>" class="p-2" alt="<?php echo $image['alt']; ?>" />
    					<?php endforeach; ?>
    					<?php endif; ?>
    					<?php endwhile; ?>
    					<?php else : ?>
    					// no images found
    		<?php endif; ?>

    Is it possible to adjust the sort order with the above code? I know with a WP Query I could write some args but not sure where I’d start with this. Any help would be appreciated. Thanks!

  • There isn’t any easy way to programmatically sort the rows of a repeater.

    ACF has an example of using array_multisort().

    There are other PHP functions as well, I generally prefer using usort().

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.