0

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 :)

Edited by gentlemedia

2
Contributors
5
Replies
27
Views
9 Months
Discussion Span
Last Post by gentlemedia
0

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 );
0

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 :)

0

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').

0

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)

Edited by gentlemedia

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.