Home > Blockchain >  Wordpress - Need to pull ALT text from images
Wordpress - Need to pull ALT text from images

Time:12-19

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 ><img src="<?php echo wp_get_attachment_url( $logo['ID'], $size ); ?>" alt=""></div>
                </div>
                <?php endforeach; ?>                
              </div>
            </div>
            <?php endif; ?>

CodePudding user response:

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

CodePudding user response:

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']; ?>">

  • Related