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]
Might be missing something, but surely just an if statement would do the job?
if(get_field('active')) {
// do something
}
Something like this?
function group_shortcode($atts)
{
$group = get_field($atts[0]);
foreach($group as $key => $value) {
print_r($value);
}
}
add_shortcode('acf_group', 'group_shortcode');
then use [acf_group GROUPNAME]
Sounds like the same issue I was having with get_sub_field – https://support.advancedcustomfields.com/forums/topic/get_sub_field-returning-wrong-field-on-acf-pro-5-7-11/
Seems like the issue was perhaps with sub-fields named the same as the field type, since the issue I had was with a field called “text”.
Seems to be working for me. Cheers.
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.