Support

Account

Home Forums Feature Requests Choose what size 'Image URL' returns Reply To: Choose what size 'Image URL' returns

  • A simple solution if the goal is to keep templates clean would be to create a helper function like

    function get_image_field_src_by_size($field_name, $size){
        
        $image_id = get_field($field_name);
        $image = wp_get_attachment_image_src($image_id, $size);
        return $image[0];
    }
    
    <div style="background-image:url(<?php echo get_image_field_src_by_size('image_field_name', 'image_size_name') ?>)"></div>
    

    Or, without a function and just a bit uglier inline

    <div style="background-image:url(<?php echo wp_get_attachment_image_src(get_field('image_field_name'), 'image_size_name')[0] ?>)"></div>