0

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:

enter image description here

kindly Assist, Thank you.

1
  • Can you var_dump($imgurl) right after you get the value? Depending on your ACF configuration you might get an array response with all the available sizes for your image instead of the ID of the media.
    – Tim
    Aug 15, 2020 at 8:06

1 Answer 1

0

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 );
}
5
  • I've already tried that. Nothing gets displayed. Nov 16, 2018 at 16:51
  • what do you see if you var_dump(get_field('user_avatar, $user->ID'));? Also, can you show how your ACF fields are configured?
    – John O
    Nov 16, 2018 at 17:11
  • I also wonder where/how $user is being defined.
    – John O
    Nov 16, 2018 at 17:15
  • this is what I get C:\wamp\www\new.usaradio.live\wp-content\themes\USA-Radio-Theme\page-profileview.php:103:null Nov 16, 2018 at 17:58
  • I also tried the below, and it just showed the image id: <?php $current_user = wp_get_current_user(); echo $current_user->banner; echo wp_get_attachment_image( get_the_ID(), array('700', '600'), "", array( "class" => "img-responsive" ) ); ?> Nov 16, 2018 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.