Skip to content

Instantly share code, notes, and snippets.

@kmwalsh
Last active September 17, 2020 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmwalsh/91897e101d37a49781e24948393316d6 to your computer and use it in GitHub Desktop.
Save kmwalsh/91897e101d37a49781e24948393316d6 to your computer and use it in GitHub Desktop.
/**
* 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