1
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 attachments.

2
  • Where are you calling the function? It will need to be inside a loop for get_post_thumbnail_id to work. Maybe global $post; $thumb_alt = get_post(get_post_thumbnail_id( $post->ID ))->post_excerpt; will help? – admcfajn Nov 20 '20 at 19:28
  • yeah it is inside, i dont know get it to work before of last update was working fine. Anyway with global $post same result. the code is good something is and i dont understand it – Mircea Gyurka Nov 20 '20 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.