Skip to content

Instantly share code, notes, and snippets.

@shrinkray
Created October 30, 2019 13:04
Show Gist options
  • Select an option

  • Save shrinkray/fb4ab0dfba74f2e275b3bbb98c8a2fde to your computer and use it in GitHub Desktop.

Select an option

Save shrinkray/fb4ab0dfba74f2e275b3bbb98c8a2fde to your computer and use it in GitHub Desktop.
Display the featured image with the alt tag using get_the_post_thumbnail_url();
/**
* To display the featured image with the alt tag
*
* @link: https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
* @author: https://profiles.wordpress.org/thelilmercoder/
*/
$thumbnail = get_the_post_thumbnail_url();
if ( $thumbnail ) {
$alt_text = get_post_meta( $thumbnail->ID, '_wp_attachment_image_alt', true );
if ( ! empty( $thumbnail ) ) {
if ( ! empty( $alt_text ) ) {
$alt_text = $alt_text;
} else {
$alt_text = __( 'no alt text set', 'textdomain' );
}
echo '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment