Reputation:
My alt tags for images don't seem to work. I can add an alt tag in the admin for a particular image but I can't seem to display that.
My alt tag just remains empty.
Now I've been looking around for a solution and I've found the following:
get_post_meta($attachment_id, '_wp_attachment_image_alt', true)
but that doesn't seem to work at all, the alt tag just stays empty.
At the moment my code is the following:
<img src="<?php the_sub_field('image'); ?>" title="<?php the_title(); ?>" alt=""/>
Any solutions for this?
I presume there is a standard wordpress function to do this, since we have the option to insert an alt tag in the admin area
Upvotes: 0
Views: 2233
Reputation: 12524
Judging from the code you post on your fiddle in the comments, I would say you need to change the way you have setup your ACF fields.
get_post_meta($attachment_id, '_wp_attachment_image_alt', true)
won't work because $attachment_id
is empty.
It looks like you have set your ACF Fields up to return the image URL instead of the image object or the image ID. I would suggest changing the field to return the ID. Then you can modify your code like the following:
<div class="row bottom-margin-half">
<?php $i=0 ?>
<?php $teamMembers=count(get_sub_field( "persoon")); ?>
<?php while (the_repeater_field( 'persoon')) { ?>
<?php if ($i==0 || $i % 4===0 ) { ?>
<div class="row">
<?php } ?>
<div class="col col-lg-3 col-sm-4 teamdescription">
<?php echo wp_get_attachment_image( get_sub_field( 'image' ), 'full', false, array( 'class' => 'r5' ) ); ?>
<p><b><?php the_sub_field('naam'); ?> <?php the_sub_field('linkedin'); ?></b>
</p>
<p>
<?php the_sub_field( 'functie'); ?>
</p>
</div>
<?php $i++; ?>
<?php if ($i==0 || $i % 4===0 || $teamMembers===$i) { ?>
</div>
<?php } ?>
<?php } ?>
</div>
The wp_get_attachment_image
function creates the image element and handles the alt tag for you.
Upvotes: 0
Reputation: 2101
May be you are using Chrome Browser, it's chrome's issue.
or if not than try this one, it will definitely helps you.
<img src="<?php the_sub_field('image'); ?>" title="<?php the_title(); ?>" alt="image"/>
Thanks.
Upvotes: 0
Reputation: 2287
Try it this way: (not yet tested)
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title = $attachment->post_title;
Which leads you to:
<img src="<?php the_sub_field('image'); ?>" title="<?php echo $image_title; ?>" alt="<?php echo $alt; ?>"/>
Upvotes: 0