admin管理员组

文章数量:1122846

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.

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.

Share Improve this question edited Nov 21, 2018 at 14:46 Wilco 2992 gold badges5 silver badges14 bronze badges asked Nov 16, 2018 at 15:15 Xhanti MdanaXhanti Mdana 11 silver badge1 bronze badge 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 Commented Aug 15, 2020 at 8:06
Add a comment  | 

1 Answer 1

Reset to default 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 );
}

本文标签: Display custom image field in user profile