skip to Main Content

Can Somebody Help how to pull alt text from wordpress images in the following code??

<?php $logos = get_field('upload_logos');
            $size = 'full';

            if( $logos ): ?>
            <div>
              <div>
                <?php foreach( $logos as $logo ): ?>
                <div>
                  <div class="ccrc-logo-box-inner"><img src="<?php echo wp_get_attachment_url( $logo['ID'], $size ); ?>" alt=""></div>
                </div>
                <?php endforeach; ?>                
              </div>
            </div>
            <?php endif; ?>

2

Answers


  1. You are using the wp_get_attachment_image() function to output your url

    Documentation of wordpress tells us you can pass the alt as a parameter to fetch and output it:

    https://developer.wordpress.org/reference/functions/wp_get_attachment_image/#parameters

    Login or Signup to reply.
  2. You are using get_field() function, so I guess you have ‘Advanced Custom Fields’ plugin. You want to get the data of a field with type of image. So looking at the documentation:
    https://www.advancedcustomfields.com/resources/image/

    You can access the alt text from the field object, the same way you already did it with $logo['ID'] before.

    <img src="<?php echo wp_get_attachment_url( $logo['ID'], $size ); ?>" alt="<?php echo $logo['alt']; ?>">

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search