Skip to content

Instantly share code, notes, and snippets.

@nanoanno
Last active March 26, 2021 21:08
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 nanoanno/76a40a52ba96243f889b63382f6307f0 to your computer and use it in GitHub Desktop.
Save nanoanno/76a40a52ba96243f889b63382f6307f0 to your computer and use it in GitHub Desktop.
Get ACF image with alt text (and add classes)
// Load in ACF image with alt text
function get_acf_image_with_alt( $imagefield, $postID, $imagesize = 'full', $subfield = false, $class = '' ) {
if ( $subfield == true ) {
$imageID = get_sub_field( $imagefield, $postID );
}
else {
$imageID = get_field( $imagefield, $postID );
}
$image = wp_get_attachment_image_src( $imageID, $imagesize );
$alt_text = get_post_meta( $imageID , '_wp_attachment_image_alt', true );
return '<img src="' . $image[0] . '" class="' . $class . '" alt="' . $alt_text . '" />';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment