3
votes

I am trying to display an image caption for a wordpress Advanced Custom Fields (ACF) repeater field, and haven't had any luck with the follow three options:

<?php if($middle_image['image']): ?>

  <?php $midimage = wp_get_attachment_image_src($middle_image['image']); ?>

  <?php $caption = $midimage->post_excerpt; ?>

  <?php $captiontwo = $middle_image['image']['caption'] ?>

  <?php $captionthree = $middle_image['image']->post_excerpt; ?>

  <?php $alt = get_post_meta($middle_image['image'], '_wp_attachment_image_alt', true); ?>

  <?php $main_image = wp_get_attachment_image_src($middle_image['image'], 'two-column-cropped' ); ?>
    <div class="two-column-cropped"><img src="<?php echo $main_image[0]; ?>" alt="<?php echo $alt ?>" />
       <?php if($caption) { ?>
          <div class="image_caption"><?php echo $caption; ?></div>
       <?php } ?>
    </div>

  <?php endif; ?>

Any suggestions would be appreciated. Thanks.

1

1 Answers

0
votes

wp_get_attachment_src() does not fetch any data about an image besides the URL, height and width. You might want wp_get_attachment_metadata() however I think you would be better off changing your ACF field to return an Image Object (actually an array) instead of the Image ID (as I assume you have it now).

ACF can return any of three things for an image field: The image src URL, the Attachment ID (which can be passed to functions such as wp_get_attachment_image_src()), or the attachment information as an array.

You can check that you are receiving the correct response from ACF by using var_dump($middle_image) or var_dump($midimage)

I assume that $middle_image is your repeater field.

As long as ACF is configured correctly to return the image object (instead of the image URL or ID), you can simply remove this line:

<?php $midimage = wp_get_attachment_image_src($middle_image['image']); ?>

and then access the image caption using $middle_image['image']['caption']