Get the value for ALT is we check to see if an actual ALT value is set, if not we use the caption and if no caption is set we use the title

Question
function custom_pic_alt( $attachment_ID ) {

  // Get ALT
$thumb_alt = get_post_meta( $attachment_ID, '_wp_attachment_image_alt', true );

// No ALT supplied get attachment info
if ( empty( $thumb_alt ) )
  $attachment = get_post( $attachment_ID );

// Use caption if no ALT supplied
if ( empty( $thumb_alt ) )
  $thumb_alt = get_post(get_post_thumbnail_id())->post_excerpt;

// Use title if no caption supplied either
if ( empty( $thumb_alt ) )
  $thumb_alt = get_post(get_post_thumbnail_id())->post_title;

// Return ALT
return esc_attr( trim( strip_tags( $thumb_alt ) ) );
}

show result

<?php echo custom_pic_alt( 10 ); ?>

and i get same alt for all attachment.

0
Mircea Gyurka 2 months 0 Answers 11 views 0

Leave an answer