I'm not sure why it returns Array, because as far as I know I don't retrieve the profile images from an array. See the following function.

        function getTeam() {

            global $post;
            $team = new WP_Query();
            $team->query('post_type=coaches');

            if ($team->found_posts > 0) {
                echo '<ul>';
                    while ($team->have_posts()) {
                        $team->the_post();
                        $listTeam = '<li>';
                        $listTeam .= '<a href="#" class="js-modal">';
                        $listTeam .= '<figure class="profile-img">';
                        $listTeam .= '<img src="' . get_field('profile-image') . '" alt="">';
                        $listTeam .= '<figcaption><span>Bekijk profiel</span></figcaption>';
                        $listTeam .= '</figure>';
                        $listTeam .= '</a>';
                        $listTeam .= '<p>' . get_field('biography') . '</p>';
                        $listTeam .= '</li>';
                        echo $listTeam;
                    }
                echo '</ul>';
                wp_reset_postdata();
            } else {
                echo '<p>No team members found</p>';
            }

        }

I've googled and found only two similar questions and in one of them a poster said by default images are returned as an array

So I added two variables and tried to retrieve the image like this:

$image = get_field('profile-image');
$profileImage = $image['url'];

$listTeam .= '<img src="' . $profileImage . '" alt="">';

But then I also get an empty value like this img src="" alt="" /> in my HTML.

For the rest I checked if the paths are right and they are and I don't get any errors. Thus... I'm a bit stuck here :)

Member Avatar for diafol

Yes a strange matter. Not sure how your data is stored. If via media manager, then your field should be stored serialized, so when you retrieve it, it will be as an array, containing data such as: id, url, title, caption etc.

$img = get_field('profile-image');
//you can then build up a custom img tag including all the attributes you want - OR -
//you can get WP to do it for you:
$size = 'full';
wp_get_attachment_image( $img, $size );

To check what's in the $img var, do:

var_dump( $img );

Thanks, Diafol!
But unfortunately that didn't work either. var_dump returned this bool(false)

I found a whole section in the documentation about retrieving an image with ACF and will see if I can find a solution in there.
https://www.advancedcustomfields.com/resources/image/

will keep you posted :)

Ah... I had to change a setting in the ACF plugin,. Instead of returning the value as an Object (the default setting), I had to return it as an URL.

All good now :)

Member Avatar for diafol

If you return as an URL, usually by using the_field('profile-image'), you can't then access the other attributes - unless you've already done a get_field('profile-image').

When returning as an image URL I had to use get_field()as well. Not sure why, but the_field()returned the path to the image just as text.

I know that I might need in the future more data such as that from the alt attribute or the sizes when I need those for cropping. I tried several ways (from the examples in the ACF documentation) to return as an image Object, but for some reason I coudn't get that right. For everything what I did var_dump returned bool(false)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.