Support

Account

Home Forums Add-ons Repeater Field User Field In A Repeater

Solved

User Field In A Repeater

  • Hi,

    I’m trying to get a User from a User field to show on the front end. It’s in a Repeater. The setup is a Repeater field called ‘staff_notes’ and a User sub_field called ‘staff_member’

    What I want is to output the display name with a link to the user’s profile, but even just getting the name would be a start. I’ve looked at the documentation and this is the code I tried:

    <?php
    
    if( have_rows('staff_notes') ): ?>
    
    <?php while ( have_rows('staff_notes') ) : the_row(); ?>
    
    <?php $user = get_sub_field('staff_member');
    
    if( $user ): ?>
    
    <h3><?php echo $user['display_name']; ?></h3>
    
    <?php endif; ?>
    
    <?php endwhile; ?>
    
    <?php endif; ?>

    The User sub_field is using the Array return type with single select, so I saw this code here:

    https://www.advancedcustomfields.com/resources/user/

    $user = get_field("user_field");
    if( $user ): ?>
    <div class="author-box">
        author-avatar
        <h3><?php echo $user['display_name']; ?></h3>
        <?php if( $user['user_description'] ): ?>
            <p><?php echo $user['user_description']; ?></p>
        <?php endif; ?>
    </div>
    <?php endif; ?>

    But even changing get_field to get_sub_field (since it’s in a Repeater) didn’t seem to work.

    That’s why I added the code for while (have_rows), but I’m still not getting results. I also tried changing the return type (object and ID) and tried changing the User field to a multiselect and using the code for that on the above URL.

    How would I get the User’s display name with a link to their profile with the setup I have? As a reminder, the User field is a sub_field in a Repeater.

    Thanks, Chris.

    —- Versions —-
    ACF v5.8.7 (PRO)
    WP v5.3.2

  • I did actually manage to get this working, for displaying the display_name at least. The final code I used was:

    <span class="staff-note-label">Note Added By:</span>
    <?php
    $user = get_sub_field("staff_member");
    
    if( $user ): ?>
    
    <span class="staff-note-name"><a href="<?php echo $user['user_url']; ?>"><?php echo $user['display_name']; ?></a></span>
    
    <?php endif; ?>

    I’m unable to get the url to the User’s profile though. The url is showing as the current post url. I’m sure that user_url is the right meta and I noticed there’s code for the user_url in the multiple selected users example at:

    https://www.advancedcustomfields.com/resources/user/

    But I tried it with a multi select user field as well and used the second example code and the link is still for the current post and not the user profile url. How can I get the link for the user’s profile in this context?

    Thanks.

  • It does not appear that ACF is returning the url, the value is there but it’s empty

    Try

    
    echo get_author_posts_url($user['ID']);
    
  • Thank you so much John, that was it. Mystery solved!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.