0

I have one problem. I can't get the alt tag inside me repeater fields in ACF, I've tried multiple methods and nothing works for me, could you please help me? I can't fix this problem for a week :( Something that I've tried and it should be working was this:

<?php while( have_rows('logo_list') ) : the_row();?>
<?php $image = get_sub_field('image'); ?>
  <li><img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>"></li>
<?php endwhile;?>

And it went this result: enter image description here What am I doing wrong?

The basic code was like this:

<?php while( have_rows('logo_list') ) : the_row();?>
     <li><img src="<?php echo get_sub_field('image');?>" alt></li>
<?php endwhile;?>

Images format set as Array Can you please help? I really don't know what to do :(

my fields enter image description here

enter image description here

The results of output enter image description here

FIELD SETTINGS enter image description here enter image description here enter image description here

2 Answers 2

0

See here for ACF image docs.

https://www.advancedcustomfields.com/resources/image/

If image field is set to return as an array then you need store the image subfield into a variable and use the variable to output the array keys.

So in your case, it would be like this...

<?php while( have_rows('logo_list') ) : the_row();?>
     <?php $image = get_sub_field('image'); ?>
     <li><img src="<?=$image['url']?>" alt="<?=$image['alt']?>" /></li>
<?php endwhile;?>

Use the code below inside your loop to dump your $image variable data so you can see all the image sizes and other image data.

echo '<pre>' . print_r($image, true) . '</pre>';

Very strange what is happening here.

Before your loop try adding this to see all the data in the field.

<?php global $post; ?>
<?php $field = get_field('logo_list', $post->ID); ?>
<?php echo '<pre>' . print_r($field, true) . '</pre>'; ?>

Post the output when you've done this.

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Howdy_McGee
    Jul 25, 2022 at 1:49
0

I have an alternative solution. Instead of using the image array in your ACF field, use the image id. Then use:

<?php $image_id = get_sub_field('image'); ?>

<?php echo wp_get_attachment_image( $image_id, 'shop_thumbnail' ) ; ?> // replace 'shop_thumbnail' with the size you want rendered.

The reason I recommend this method is

a) you can still fetch any data you want to pull out via the image id,

b) WordPress will automatically pull the alt for you,

c) WordPress will now be able to use srcsets to deliver appropriate image sizes for devices, and

d) plugins that are designed to use webp or modern image files are more compatible.

1
  • thank you! I used url but this also works, thanks!
    – Tanya Nami
    Aug 3, 2022 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.