WordPress: Show title and alt on images

To add image alt and title attributes to images, add the following to functions.php:
function add_image_title( $attr, $attachment= null ) {
    $img_title= trim( strip_tags( $attachment->post_title ) );
    $attr['title'] = $img_title;
    $attr['alt'] = $img_title;
add_filter( 'wp_get_attachment_image_attributes','add_imgage_title', 10, 2 );
or replace $attr[‘title’] with:
$attr['title'] = the_title_attribute( 'echo=0' );

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>