SrcSet Images in WordPress using Advanced Custom Fields

One of the problems I came up against recently was how to mimic the way in which WordPress now creates srcset responsive images dynamically for all post content images and featured images using an ACF field. To achieve this here’s an example below, we’re simply grabbing the original src image attached to that custom field, then the srcset versions of that image, while doing all this we’re also grabbing the WordPress Media Library Alt text just to make the images SEO friendly.

When setting up your ACF Custom Fields make sure you set the image field to return as ID rather than array otherwise the below snippet won’t work.

Code Example:

I hope this snippet helps somebody out.