Display custom image field in user profile

I am trying to display user info from acf so far the normal text, radio and select and date fields display if I use the following:

?php $current_user = wp_get_current_user(); echo $current_user-user_gender; ?

I have created 2 image custom fields one of each I would like to use as an avatar. Ho do I get the images to display if the custom field name is user_banner and user_avatar.

the fields are custom fields from acf are user_banner and user_avatar.

Here's the full code for the page template:

?php
/**
    Template Name: View Profile
*/

get_header();?
section class="page-profile"
    div class="container-fluid"
        div class="row"
            div class="col-sm-4"
                div class="profile-user-info"
                    div class="profile-info-row"
                        div class="profile-info-name" Username /div

                        div class="profile-info-value"
                            span?php $current_user = wp_get_current_user(); echo $current_user-user_login; ?/span
                        /div
                    /div
                    div class="profile-info-row"
                        div class="profile-info-name" First Name /div

                        div class="profile-info-value"
                            span?php $current_user = wp_get_current_user(); echo $current_user-user_firstname; ?/span
                        /div
                    /div
                    div class="profile-info-row"
                        div class="profile-info-name" Last Name /div

                        div class="profile-info-value"
                            span?php $current_user = wp_get_current_user(); echo $current_user-user_lastname; ?/span
                        /div
                    /div

                    div class="hr hr-8 dotted"/div

                    div class="profile-info-row"
                        div class="profile-info-name" Email /div

                        div class="profile-info-value"
                            a href="mailto:?php $current_user = wp_get_current_user(); echo $current_user-user_email; ?"?php $current_user = wp_get_current_user(); echo $current_user-user_email; ?/a
                        /div
                    /div  

                    div class="hr hr-8 dotted"/div

                    div class="profile-info-row"
                        div class="profile-info-name" Gender /div

                        div class="profile-info-value"
                            span?php $current_user = wp_get_current_user(); echo $current_user-user_gender; ?/span
                        /div
                    /div

                    div class="profile-info-row"
                        div class="profile-info-name" Age /div

                        div class="profile-info-value"
                            span?php $current_user = wp_get_current_user(); echo $current_user-user_birthday; ?/span
                        /div
                    /div

                    div class="profile-info-row"
                        div class="profile-info-name" Country/div

                        div class="profile-info-value"
                            span?php $current_user = wp_get_current_user(); echo $current_user-user_country; ?/span
                        /div
                    /div
                /div

                ?php

                $imgurl = get_field('user_banner',$user-ID);

                if (filter_var($imgurl, FILTER_VALIDATE_URL) === FALSE)
                {
                  $imgurl = wp_get_attachment_url($imgurl);
                }
                   echo 'img src="' . $imgurl . '" alt="image"';
                ?

                ?php

                $imgurl = get_field('user_avatar',$user-ID);

                if (filter_var($imgurl, FILTER_VALIDATE_URL) === FALSE)
                {
                  $imgurl = wp_get_attachment_url($imgurl);
                }
                   echo 'img src="' . $imgurl . '" alt="image"';
                ?

            /div
        /div
    /div
/section
?php get_footer(); ?      

The code for the image used I got somewhere but when I inspect it shows the below:

kindly Assist, Thank you.

Topic advanced-custom-fields user-meta Wordpress

Category Web


Check out the documentation for ACF image fields: https://www.advancedcustomfields.com/resources/image/

When you use get_field on an image field, it looks like it returns the ID for the image, not a URL. This might have been different in a previous version of ACF.

I think something like this might work:

$image = get_field('user_avatar', $user->ID');
$size = 'full'; // (thumbnail, medium, large, full or custom size)

if( $image ) {
    echo wp_get_attachment_image( $image, $size );
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.