Archived topic
Dynamic Data & ACF User Field Array - how do I show a user's display name?
3 replies · Started by Tim on March 7, 2023
Using advanced custom fields, generatepress pro, and generate blocks pro
Custom field type: User field (https://www.advancedcustomfields.com/resources/user/)
I have the return format set to "User Array"
I am trying to display the user-selected's display name as dynamic data. Currently, I can only get it to display the user ID.
Is this possible? Any help would be appreciated.
Hi Tim,
Currently, GB doesn't support array value or multiple values from ACF unfortunately.
As you are seeing the id, have you changed the return value to be user id instead of user array?
If so, try the solution below:
1. Add an additional CSS class to the headline block, eg. my-user.
2. Then add this PHP code to replace the id with the display name, change co-author to your meta field name.
add_filter( 'render_block', function( $block_content, $block ) {
if ( ! empty( $block['attrs']['className'] ) && 'my-user' === $block['attrs']['className'] ) {
$user_id = get_post_meta(get_the_id(), 'co-author', true);
$display_name = get_the_author_meta( 'display_name', $user_id );
$block_content = str_replace($user_id, $display_name, $block_content );
}
return $block_content;
}, 10, 2 );
Adding PHP: https://docs.generatepress.com/article/adding-php/
This was helpful. Thank you!
You are welcome :)