0

I have a php code. That put the thumbs on the screen. I have this:

<?php if (has_post_thumbnail()) : ?>
<div class="image">
    <?php
        $attachment_id = get_post_thumbnail_id();
        $image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' )
    ?>
    <img src="<?php echo $image_attributes[0]; ?>" width="186" height="137" alt="">
    <?php //the_post_thumbnail('default_product_img'); ?>
</div><!-- /image -->
<?php endif; ?>

The code is good. The images are showing and that is good. But now i have a problem. I can not putt the alt text in the image.

How can i add the alt text of the image, in the alt tag of the image?

Thank you for helping

2 Answers 2

1

This simply do the same work and also output the alt value:

<?php if (has_post_thumbnail()) : ?>
<div class="image"><?php the_post_thumbnail( array(186, 137) ); ?></div>
<?php endif; ?>

See Codex

2
  • Thank you. This is working. How can i remove the attachment-186x137 wp-post-image classes of the image? Aug 28, 2013 at 9:36
  • <?php echo str_replace(' wp-post-image', '', get_the_post_thumbnail( null, array(186, 137), array('class'=>'') ) ); ?>
    – gmazzap
    Aug 28, 2013 at 9:51
0

I think you need the function wp_get_attachment_image

<?php 
if (has_post_thumbnail()) : 

 $attachment_id = get_post_thumbnail_id();
 $attr_args = array(
    'class' => 'overriden-class',
    'alt'   => 'override-if-required'
 );
 $size = array(186,137);
 echo '<div class="image">';
 echo wp_get_attachment_image($attachment_id,$size,$icon=false,$attr_args);
 echo '</div>';
endif;


?>

If you need the original alt tag remove the alt key from the $attr_args array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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