Home › Forums › General Issues › show custom field for media (img) in theme › Reply To: show custom field for media (img) in theme
To use it in a theme you can just do
$img = get_field('image_source');
echo "<img src='" . $img['url'] . "' alt='" . $img['alt'] . "'>";
You can replace (or add srcset attributtes) by replacing $img['url']
with $img['sizes']['large']
, $img['sizes']['small']
, etc.
If you wanted to use it actually in a post (as in within the WP editor), your best bet would likely be to create a shortcode in your theme’s functions.php, which I expect would look something like the following:
function acf_image_function($atts) {
$img = get_field($atts[0]);
echo "<img src='" . $img['url'] . "' alt='" . $img['alt'] . "'>";
}
add_shortcode('acf_image_shortcode', 'acf_image_function');
EDIT: then you’d use the shortcode [acf_image_shortcode image_source]
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.