3

Trying to pull alt and title tag for a image via ACF using the image array. This is what I have been doing and it seems to work most of the time but I can't get it to work this time.

Here is my code.

See anything off?

<div class="slide-img">
  <?php
  $image = get_field('slide_image_1');
  ?>
<img class="rsImg" src="<?php echo $image; ?>" alt="<?php echo $image['alt']; ?>" title="<?php echo $image['title']; ?>">
</div>

slide_image_1 is my ACF name

Aibrean
  • 6,297
  • 1
  • 22
  • 38
Dan Crowe
  • 115
  • 13

2 Answers2

2

try:

<pre> 
<?php print_r($image); ?> 
</pre> 

to output an image object and see if the image was actually supplied by acf and which image object/array fields are available. If alt and other image fields are there, then it should be working

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
Nick Surmanidze
  • 1,671
  • 1
  • 11
  • 20
  • BTW you are echoing an $image in src. If you have an image array than you should be echoing $image['url'] or url or whatever is supplied. – Nick Surmanidze Apr 26 '16 at 21:07
2

When you echo the image field directly, <?php echo $image; ?> you are simply outputting the whole array, given that you set the image field to output Image Object, when you made the field.

You need to add the object you need from the array, as you did with the alt and title, so your final image tag will be:

<img class="rsImg" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" title="<?php echo $image['title']; ?>">

Always try var_dump or print_r as the other answer mentioned, when you see something not working, this way you'll be able to see what's returned and grab the correct data you need.

Matias Vad
  • 1,683
  • 1
  • 18
  • 28