Created
February 4, 2021 08:51
-
-
Save thisbit/a8f97e0035d56a85cff7bda023af6044 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function myprefix_get_attachment_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 = $attachment->post_excerpt; | |
// Use title if no caption supplied either | |
if ( empty( $thumb_alt ) ) | |
$thumb_alt = $attachment->post_title; | |
// Return ALT | |
return esc_attr( trim( strip_tags( $thumb_alt ) ) ); | |
} | |
// possible way of using | |
?> | |
<img src="..." alt="<?php echo myprefix_get_attachment_alt( 10 ); ?>"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment