We want to replace a slider with a clickable random image. So far I have half of this working using the below code that pulls one image from the ACF gallery and displays it.
What I can’t seem to find is how to then use the image’s custom link (as assigned when looaded into the WP media gallery), so that the image is also clickable (where the user loads the image and assigns the desired custom link).
Anyone have any idea how to get this to work?
Cheers!
<?php
$images = get_field('gallery', '', false);
$size = 'full';
$rand = array_rand($images ?? [null]);
if( $images ): ?>
<?php echo wp_get_attachment_image( $images[$rand], $size, "", $attr ); ?>
<?php endif; ?>
2
Answers
Solved! For those that want the solution, here is the code I bashed together to make an ACF Repeater powered random image with a custom link. Thanks @MindBorn for getting my brain working :)
By default in
WordPress
attachments like images have four editable text fields available:Probably not a good practice but for the record if you were to use one of these fields as the source for your image’s custom link here’s how to quickly access them:
You haven’t exactly clarified that but I suspect that you’re actually using
ACF
fields group with image link custom field assigned to the attachment’s form. I’d guess its type would be url then. If this is the case, you can simply access this field’s value like this:Then it’s just a matter of making your image clickable with the new url: