Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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