Support

Account

Home Forums ACF PRO Custom gallery image link

Solved

Custom gallery image link

    • Doliwa

    • March 27, 2018 at 7:28 pm

    I would like to add external links to the pictures in the gallery.
    I created my own text field “image_custom_url” and assigned them to atachments. Currently, the template code looks like:

    <div id="content">
    					<div class="row main">
    						<div class="small-12 column custom-page">
    							<div id="block-system-main" class="block block-system">
    								<div class="static-page">
    									
    									<p><?php the_field('partners_description'); ?></p>
    									<p class="larger"><?php the_field('short_title'); ?></p>
    
    									<div class="row partners-wrapper">
    										<?php 
    										$images = get_field('partners_icons');
    
    										if( $images ): ?>
    										    
    									        <?php foreach( $images as $image ): ?>
    								                <div class="large-3 medium-4 small-12 columns">
    								                	<div>
    									                	<a class="uk-inline" href="<?php echo $image['url']; ?>" caption="Caption 1">
    									                		<img src="<?php echo $image['url']; ?>" alt="">
    									                	</a>
    									                </div>
    								                </div>
    									        <?php endforeach; ?>
    
    										<?php endif; ?>
    									
    									</div>
    								</div>
    							</div>
    						</div>
    					</div>
    				</div>

    How can I add my own link to photos?

  • Each image has an image ID, you use this ID to get the custom fields.

    Example:

    
    foreach ($images as $image) {
      $value = get_field('your custom field name', $image['ID']);
    );
    

    or something like:

    
    foreach ($images as $image) {
      ?>
        <a href="<?php the_field('your link field name here', $image['ID']; ?>">something here</a>
      <?php 
    );
    
    • Doliwa

    • March 28, 2018 at 12:50 am

    Thank you works correctly

  • Is there a way to add a link on each gallery image per instance of a gallery, rather than only once per media attachment?

    Eg, ‘About Us’ has a gallery that uses the image ‘Frog’ that links to google.com.
    ‘Contact Us’ has a gallery that uses the same ‘Frog’ image, but I want that one to link to bing.com.

  • Not using the Gallery field. To do something like this you’d need to do something like use a repeater field and add another field for the custom link. The gallery field only holds a list of attachments IDs from the media library, any information you get would be assigned to the attachment.

  • Had the same query and stumbled on this page.
    I found a solution that works perfectly for anyone else that ends up here.

    1. Create a new ACF Field Group e.g. “Media Metadata”
    2. Add a field as a Link or Page Link or URL (I used Page Link)
    3. Then under Location => Rules “show this field group if” = Attachment
    4. This will add a new link field into each media library item
    (https://www.greengeeks.com/tutorials/article/add-custom-fields-to-media-wordpress/)

    I used a Page Link and added links to my Media in the Media Library.
    So when calling it in my Gallery loop code:

    <?php
    foreach( $images as $image ): 
      
      $link_id = get_post_meta( $image['id'], 'page_link', true); ?>
      
      <a href="<?php the_permalink( $link_id ); ?>" title="<?php echo $image['alt']; ?>">
        <img data-src="<?php echo $image['sizes'][$size]; ?>"/></a>
    
    <?php				  
    endforeach; 
    ?>
  • Hi

    Quick question where do i add the php to? I have got the attactment part working in the image media serttings but its not clicking through to the custom link.

    thanks

    Danny

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.