John Davidson

php - ACF user field with custom field attached to user

0 comments
Message:


I'm using ACF "user" field to attach a user to a post. The user has a series of custom fields like "short-description".


The code below us able to show the correct user name but unable to display the short-description. Any ideas what I might be doing wrong?


<?php $user = get_field("seller"); if( $user ): ?>
<h3><?php echo $user['display_name']; ?></h3>
<?php the_field('short-description'); ?>
<?php endif; ?>

This code don't work either:


<?php the_field('short-description', $user); ?>

Many thanks



My Answer:
(Advanced Custom Fields) in WordPress to create a custom user field. To attach a custom field to a user, you can use the following code:

php
// Add custom field to user profile
function add_custom_user_field() {
acf_add_local_field_group(array(
'key' => 'group_user_custom_field',
'title' => 'Custom User Field',
'fields' => array(
array(
'key' => 'field_custom_field',
'label' => 'Custom Field',
'name' => 'custom_field',
'type' => 'text',
),
),
'location' => array(
array(
array(
'param' => 'user_form',
'operator' => '==',
'value' => 'all',
),
),
),
));
}
add_action('acf/init', 'add_custom_user_field');


This code will add a custom field labeled "Custom Field" to the user profile page in the WordPress admin. You can customize the field type and label as needed.

Rate this post

4 of 5 based on 6981 votes

Comments




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