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>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.