Hi, I have literally been working on this for days, I'm a complete novice so just kind of trying everything to get this to work so forgive me if there are lots of unnecessary bits in my code.
I'm grabbing a client logo from a WordPress custom field group called clients, there are currently 72 in the list but this will change as some get added and others get deleted. I want the logos to display in a grid of 9 tiles <li>'s
But not showing the same logo twice in the grid (ever). So what I have tried is to divide he logos into chunks to show in each <li> but I'm still seeing duplicates, in a var_dump I can see that all the logos displayed withing each <li> are unique so I can only guess when it shuffles it's changing the chunks? I'm not sure but if anyone could improve on what I have done it would help me a ton.
<?php
$clients = $_w->page->get_field('clients');
// Get the total number of clients
$num_clients = count($clients);
// Set the number of logos to display in the grid
$num_logos = 9;
// Get a list of unique random indices within the range of the clients array
$indices = UniqueRandomNumbersWithinRange(0, $num_clients - 1, $num_logos);
// Sort the indices in row-major order
sort($indices);
// Create an array to store the unique images
$images = array();
foreach ($clients as $c) {
$images[] = $c->image->url;
}
// Loop through the indices and get the corresponding image URLs
foreach ($indices as $i) {
$image_id = $clients[$i]->image->id;
$image = wp_get_attachment_image_src($image_id, 'full');
$images[] = $image[0];
}
?>
<?php
// Get an array of all image URLs
$images = array_map(function ($c) {
return $c->image->url;
}, $clients);
// Remove duplicates from the images array
$images = array_unique($images);
?>
<?php
// Loop through the indices and get the corresponding image URLs
foreach ($indices as $i) {
$image_id = $clients[$i]->image->id;
$image = wp_get_attachment_image_src($image_id, 'full');
$images[] = $image[0];
}
if ($images):
// divide the array of logos into 9 smaller arrays
$chunks = array_chunk($images, ceil(count($images) / 9));
// shuffle the chunks to get a random distribution of logos
shuffle($chunks);
?>
<section class="image-grid-container">
<script id="hd_client_gallery" type="text/json">
[<?php
$total = count($clients);
$count = 0;
foreach ($clients as $c):
echo "{";
echo '"url": "' . $c->image->url . '",';
echo '"href": "' . $c->link. '"';
echo "}";
if(++$count !== $total) {
echo ", ";
}
endforeach;
?>]
</script>
<ul class="object image-grid gallery">
<?php for ($i = 0; $i < 9; $i++): ?>
<?php if (isset($chunks[$i])): ?>
<li>
<?php foreach ($chunks[$i] as $image_url): ?>
<?php
// get the client with this image URL
$client = null;
foreach ($clients as $c) {
if ($c->image->url == $image_url) {
$client = $c;
break;
}
}
?>
<?php if ($client): ?>
<a <?= ($client->link ? 'href="'.$client->link.'"' : '' ); ?>>
<img class="img-fluid" src="<?= $image_url ?>" />
<img style="display:none;" class="img-fluid" src="" />
</a>
<?php endif; ?>
<?php endforeach; ?>
</li>
<?php endif; ?>
<?php endfor; ?>
</ul>
</section>
<?php endif; ?>
<?php
function UniqueRandomNumbersWithinRange($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
return array_slice($numbers, 0, $quantity);
}
?>
</div>
</section>