Last active
September 17, 2020 18:41
-
-
Save kmwalsh/91897e101d37a49781e24948393316d6 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
/** | |
* Get accessible post thumbnail images - fills alt attribute with post title if alt attribute is empty | |
* | |
* @param [type] $id The post ID being called. | |
* @param [type] $size The size of the image to call. Default thumbnail. | |
* | |
* @return void | |
*/ | |
function a11y_images_with_alt( $id, $size = 'thumbnail' ) { | |
$alt = get_post_meta( get_post_thumbnail_id( $id ), '_wp_attachment_image_alt', true ); | |
if ( empty( $alt ) ) : | |
$alt = get_the_title(); | |
endif; | |
echo '<a href="' . esc_url( get_the_permalink() ) . '">' . get_the_post_thumbnail( get_the_ID(), $size, array( 'alt' => $alt ) ) . '</a>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment