Support

Account

Home Forums ACF PRO How to user srcset with image ID

Helping

How to user srcset with image ID

  • If someone can help me with this, it would be much appreciated! I’m using ACF image IDs and two sizes: 220px 440px for a profile pic. I need to know how to use srcset to automatically switch between them.

    So here’s a normal scrset example with src included too:

    <img srcset=”large.jpg 440w, small.jpg 220w”
    sizes=”not sure how to do this part” src=”small.jpg” alt=”A rad wolf” />

    So would it look like:

    <?php $profile = wp_get_attachment_image_src(get_field(‘profile_pic’), ‘large’,’thumbnail’); ?>
    440w, <?php echo $profile[1]; ?> 220w”
    sizes=”not sure how to do this part” src=”small.jpg” alt=”A rad wolf” />

    Am I doing this correctly? Thanks anyone!!!!

  • I have just done this, BUT I have done it in Twig, not in PHP, so you need to rewrite the twig back to php, which should be easy.

    <a href=""><img srcset="' ~ image_320 ~ ' 150w, ' ~ image_640 ~ ' 80w" sizes="(max-width:640px) 150px" src="' ~ image_640 ~ '" /></a>

    ‘ ~ is the same as ‘ . in php.

    image_320 and and image_640 are variables which contain nothing more than the file name.

    If the screen is bigger than 640 px wide, it uses the image_640 and otherwise the image_320.

    Hope this helps a bit.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘How to user srcset with image ID’ is closed to new replies.