I have galleries on my website and the link to the larger image currently takes you to the image URL. I would like to change this so that it links to the Media Attachment page?
This is what I currently have:
<?php
$images = get_field('photos');
if( $images ): ?>
<div id="gallery">
<h3>Photos</h3>
<?php foreach( $images as $image ): ?>
<a class="tooltip" href="<?php echo $image['url']; ?>">
<span>
<img src="<?php echo $image['sizes']['medium']; ?>" alt="<?php echo $image['alt']; ?>" />
</span>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
Many thanks if someone can suggest what I should change this to so that the links point to the media attachment page.
Hi @kandb
Consider using get_attachment_link(...)
which returns the URI of the attachment field. Use the following code:
get_attachment_link($image['ID']);
Change your code to the following:
<?php
$images = get_field('photos');
if( $images ): ?>
<div id="gallery">
<h3>Photos</h3>
<?php foreach( $images as $image ): ?>
<a class="tooltip" href="<?php echo get_attachment_link($image['ID']); ?>">
<span>
<img src="<?php echo $image['sizes']['medium']; ?>" alt="<?php echo $image['alt']; ?>" />
</span>
</a>
<?php endforeach; ?>
</div>
<?php endif; ?>
Thank you so much, James! That fixed things perfectly. 🙂