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! While you're at it, check out some resources Treehouse students have shared here.

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

WordPress

Image alt tag

Hi guys,

This should be fairly simple but I can't find right answer, how can I call alt tag to this image?

Thank you!

<img src="<?php the_field('main_image'); ?>" alt="" class="main-image">

3 Answers

Andrew Shook
Andrew Shook
31,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.

Hi Milan,

The documentation on the ACF website has a few examples here.

This may also be useful: http://support.advancedcustomfields.com/forums/topic/how-to-use-alt-tags/.

Hope that helps

-Rich

Thanks guys!

No problem

-Rich