I would like to use the new wordpress 4.4 function srcset with an image which comes from a custom-field like this

  <img src="<?php $bigimg = get_post_meta($post->ID, 'bg_file', true); echo wp_get_attachment_url( $bigimg );?>" alt="">

How could I get the srcset function work with it?

wp_get_attachment_image_srcset( 380, 'medium' ); 

srcset is already activated for post_thumbnails in my functions.php.

Thank you!

1 Answer 1


Just put two and two together:

<img src="<?php $bigimg = get_post_meta( $post->ID, 'bg_file', true ); echo wp_get_attachment_url( $bigimg ); ?>" srcset="<?php echo wp_get_attachment_image_srcset( $bgimg, 'medium' ) ?>"  alt="" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.