Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I use a slider to show my post thumbnails. But the problem is that the slider adds thumbnails without the alt attribute:
<img src="http://example.com/wallpaper-791556-1180x500.jpg" />

But I want to have the alt attribute, as you may already guessed. I've read a lot about it and about array, but had not succeed in fixing it. I really hope, someone would be so kind to help me.

The CODE that post images:
<div id="slider" class="grid-12"> <div class="flexslider"> <ul class="slides"> <?php $count = of_get_option('w2f_slide_number'); $slidecat =of_get_option('w2f_slide_categories'); query = new WP_Query( array( 'cat' =>$slidecat,'posts_per_page' =>$count ) ); if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?> <li> <?php $thumb = get_post_thumbnail_id(); $img_url = wp_get_attachment_url( $thumb,'full' ); //get full URL to image (use "large" or "medium" if the images too big) $image = aq_resize( $img_url, 1180, 500, true ); //resize & crop the image ?> <?php if($image) : ?> <a href="<?php the_permalink(); ?>"><img src="<?php echo $image ?>"/></a> <?php endif; ?> <div class="flex-caption"> <h3><?php the_title(); ?></h3> <?php print_excerpt(300); ?> </div> <?php endwhile; endif; ?> </li> </ul> </div> </div>

And another problem is <div class="homegallery"> <a href="http://mysite.com/whatever/"><img class="scale" src="http://mysite.com/wp-content/uploads/2013/04/slide_small.jpg"/></a> do not show alt too! How can I fix that or where shoud I look for the code of this homegallery?

SOLVED IT!

share|improve this question
add comment

2 Answers

When I test get_the_post_thumbnail (also the_post_thumbnail) on WordPress 3.5.1, the alt attribute is added as it should be. If you look at the source, you will see that get_the_post_thumbnail uses wp_get_attachment_image which does add that alt attribute. By default, that attribute is the image file name but it can be edited from wp-admin->Media to be anything you want.

Either:

  1. A plugin or your theme has already altered the output of get_the_post_thumbnail, possibly via the post_thumbnail_html filter, though I did notice other filters in the code, or
  2. Your slider or theme template doesn't work the way you think it does. Perhaps it isn't using get_the_post_thumbnail, for example.

It is difficult to say more, or to say exactly where this is going wrong, without seeing the relevant code.

Also, get_the_post_thumbnail function is not a pluggable function so I have to assume you are trying to hack a Core file, which you should not be doing.

Edit:

In the latest bit of code you posted, you can see the section that generates the image tag:

<a href="<?php the_permalink(); ?>"><img src="<?php echo $image ?>"/></a>

It isn't WordPress at all, but a customized bit of code. That is where you need to add the alt attribute.

<a href="<?php the_permalink(); ?>"><img src="<?php echo $image ?>" alt="I am so alt it hurts!" /></a>

You can create the alt attribute manually or copy the code from wp_get_attachment_image

$default_attr = array(
    'src'   => $src,
    'class' => "attachment-$size",
    'alt'   => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first
);
if ( empty($default_attr['alt']) )
    $default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption                    
if ( empty($default_attr['alt']) )
    $default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title

Or cook up your own function for creating the alt attribute.

share|improve this answer
    
Thank you, for response @s_ha_dum. I'm using slightly edited Siren theme for my site. So I pretty sure slider in it is not something unique. May be there is just a mistake in code or the author just missed that moment of adding alt attribute... What info do you need to understand my problem better? –  Private Person Apr 28 '13 at 20:39
    
I would need to see the code that is generating those links-- the ones without the alt attribute. You can start by looking in the theme's functions.php but some themes distribute functions over many different files. –  s_ha_dum Apr 28 '13 at 21:45
    
Again... "the code that is generating those links" –  s_ha_dum Apr 29 '13 at 19:50
    
@PrivatePerson : See the update –  s_ha_dum Apr 29 '13 at 20:40
add comment

You could try using javascript

jQuery('.post-thumbnail-wrapper a img').removeAttr('alt');
share|improve this answer
    
Please read the question once again. Currently there is no alt, and that's the problem, so I guess removing it would not solve it. ;) –  t f Apr 28 '13 at 15:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.