1

So I'm using ACF Image: https://www.advancedcustomfields.com/resources/image/, and outputting my image and caption as an Array.

I'd like to be able to add links within my captions e.g. '', but when I do this it literally outputs the code rather than turning the text into a link.

Is there a way to make it be a link rather than just outputting the HTML as text.

This is my code for my image:

<?php 
$image = get_sub_field('image'); if( $image ):

// Image variables.
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
$caption = $image['caption'];
?>

<img class="img-fluid" src="<?php echo esc_url($url); ?>" alt="<?php echo esc_attr($alt); ?>" />

<?php if( $caption ): ?>
  <p class="caption"><?php echo esc_html($caption); ?></p>
<?php endif; ?>
<?php endif; ?>

But when I add this to the caption box within Wordpress: This image is from Flickr.

It literally outputs the same as above rather than turning the word into a link.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • What happens if you don't esc_html on the caption? and is the Flickr part of description a link? Can you share exactly what you are typing into caption, i.e. full html if you are adding any. – Unbranded Manchester Jun 03 '20 at 10:55
  • This is exactly what I'm typing into the caption box in WordPress: Photo credit: Die Brücke über die Drina by Ebs Els on Flickr That is literally what its outputting on the page, its not turning the word Flickr into a link. I'll test it removing the esc_html part. – user13671908 Jun 03 '20 at 11:01
  • 1
    @UnbrandedManchester Removing the esc_html fixed the issue thank you! – user13671908 Jun 03 '20 at 11:07

1 Answers1

1

As you can see above I had the code:

<?php echo esc_html($caption); ?>

When I removed the 'esc_html' part then I was able to add links within the caption area. So the code for the Caption was this instead:

<?php if( $caption ): ?>
   <p class="caption"><?php echo ($caption); ?></p>
<?php endif; ?>