Site logo

[Resolved] Dynamic Data & ACF User Field Array – how do I show a user’s display name?

Home Forums Support [Resolved] Dynamic Data & ACF User Field Array – how do I show a user’s display name?

Home Forums Support Dynamic Data & ACF User Field Array – how do I show a user’s display name?

  • This topic has 3 replies, 2 voices, and was last updated 3 years ago by Ying.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2559188
    Tim

    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.

    #2559243
    Ying
    Staff
    Customer Support

    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/

    #2570151
    Tim

    This was helpful. Thank you!

    #2570220
    Ying
    Staff
    Customer Support

    You are welcome   🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.