I've created an ACF Checkbox field that I assigned to the User Profile area.
I'm using a form in order to bring up a popup with the checkboxes where users choose from the available options, the form then saves and updates the choices in user's profile. That all seems to be working perfectly fine.
But now I need to show the selected choices in User Profile template using a shortcode.
My ACF return value is set to value and I'm using the snippet exactly as per ACF's docs in order to display a list of labels.
This is my shortcode:
function get_user_profile_choices() {
$field = get_field_object('color_choices');
$colors = $field['value'];
// Display labels.
if( $colors ): ?>
<ul>
<?php foreach( $colors as $color ): ?>
<li><?php echo $field['choices'][ $color ]; ?></li>
<?php endforeach; ?>
</ul>
<?php endif;
}
add_shortcode( 'user-profile', 'get_user_profile_choices' );
I am currently just testing this with my own admin account, so I'm trying to show my choices in my profile.
I'm not getting anything. Could it be because the checkbox is in User Profile? Do I need to declare any user variable? Any ideas?
CodePudding user response:
Check this out. This way you will get the value for the currently logged-in user.
function get_user_profile_choices() {
$user_id = get_current_user_id();
$field = get_field_object('color_choices', 'user_'.$user_id);
}
add_shortcode( 'user-profile', 'get_user_profile_choices' );
CodePudding user response:
Add second parameter "user_USERID" in the function "get_field_object" in order to get value from the user profile fields.
change "1" to your user ID
$user_id = 1;
$field = get_field_object('color_choices', 'user_' . $user_id);