Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Image alt tag
This should be fairly simple but I can't find right answer, how can I call alt tag to this image?
<img src="<?php the_field('main_image'); ?>" alt="" class="main-image">
Andrew Shook31,709 Points
First you will need to go and edit the image field. On the edit screen make sure that you have selected Object for the return value of the field and not URL. Once you have made that change, save it and open up the template file where you are using the custom field. There, replace your code with this code:
<?php $image = get_field('image'); if( !empty($image) ): ?> <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /> <?php endif; ?>
If you want to see the rest of the information associated with the image you can just var_dump the image variable.