John Davidson

php - ACF dynamic image field for user is not working on webpage

0 comments
Message:


I am using ACF to show different dynamic values in the user dashboard. All the other fields are working fine. But when I want to show a dynamic image then it creates problem.
I am useing elementor pro. So I thing it is a problem with elementor pro and ACF. So I go for custom code.
At first I add a custom image field for a post, and use this code.


<?php
$image = get_field($post_image);
$pic = $image['sizes']['large'];
?>

<p><?php echo $pic; ?></p>
<?php var_dump($image);?>

and this code is working fine. But when I try to show image of a user with a code. it just shows me image id, not image. all image ACF settings are same for both post and user image field.


there is two code:
one is:


<?php
$user_id = get_current_user_id();
$user = get_user_by( 'id', $user_id);
$userimg = get_field("$realtor_image",$user);
if( $userimg ) {
echo $userimg;
} else {
echo 'empty';
}
?>


another is:


<?php
$user_id = get_current_user_id();
$user = get_user_by( 'id', $user_id);
$userimg = $user->realtor_image;
?>

<p><?php echo $user->realtor_image; ?></p>
<?php var_dump($userimg);?>

and in the screenshot, you will see those above the line and below the like.
Can anyone please help me?

Output ss:
https://i.stack.imgur.com/3QfQf.jpg

Here is my image field ss:
https://i.stack.imgur.com/vv9WD.jpg




I am trying to show the image that is different from user to user, like what ACF actually does, showing dynamic content. I can do it for posts, but not for logged-in users. It is showing me the image id rather than the actual image. I have changed Return Format, but not work.

My Answer:
php
// Add custom image field for user
function add_custom_user_image_field() {
acf_add_local_field_group(array(
'key' => 'group_user_image',
'title' => 'User Image',
'fields' => array(
array(
'key' => 'field_user_image',
'label' => 'Image',
'name' => 'user_image',
'type' => 'image',
'return_format' => 'url',
),
),
'location' => array(
array(
array(
'param' => 'user_form',
'operator' => '==',
'value' => 'all',
),
),
),
));
}
add_action('acf/init', 'add_custom_user_image_field');


_Then I use this code to display the image on the user dashboard._

php
// Display custom image field for user
function display_custom_user_image_field() {
$user_id = get_current_user_id();
$user_image = get_field('user_image', 'user_' . $user_id);

if ($user_image) {
echo 'User Image';
}
}


_You can add this code to your theme's functions.php file or in a custom plugin. Make sure to replace 'user_image' with the name of your custom image field. This should display the dynamic image for each user on their dashboard._

Rate this post

4 of 5 based on 3547 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us