Support

Account

Home Forums General Issues Displaying all usernames selected from a custom field

Solving

Displaying all usernames selected from a custom field

  • Hi guys,

    I have a custom post type for entering teams (into gaming tournaments), and then using ACF I have customized this post type. One of the fields is to select which players (users) are in the team.

    On the front end I am making a custom single-team.php and trying to display each user in the team. I’ve started with <p><?php the_field('select_players'); ?></p> as per the code examples, but it lists all the values associated with the user. I’ve dumped the values and they are:

    array(11) { ["ID"]=> int(4) ["user_firstname"]=> string(5) "Chris" ["user_lastname"]=> string(8) "Burchell" ["nickname"]=> string(5) "thrax" ["user_nicename"]=> string(5) "thrax" ["display_name"]=> string(5) "thrax" ["user_email"]=> string(22) "thrax@playtopia.com.au" ["user_url"]=> string(0) "" ["user_registered"]=> string(19) "2018-01-02 04:18:15" ["user_description"]=> string(55) "Dad, Gamer, Podcaster, and founder of playtopia.com.au!" ["user_avatar"]=> string(217) "thrax" }

    Essentially I want to select each user that has been selected in the ‘selected_players’ ACF and then for each one only pull the [“nickname”] and display it… but I’m a little bit lost on how to put the code together.

    Can anyone help? 🙂

  • 
    $players = get_field('select_players');
    if ($players) {
      foreach ($players as $player) {
        echo $player['nickname'],'<br />';
      }
    }
    
  • Thanks John.. that didn’t quite have the desired effect.. it’s actually listed the first character from each ‘Player’ attribute on a new line as below.

    C
    B
    t
    t
    t
    t
    
    2
    D
    <

    Any ideas?

  • I was guessing based on what you provided.

    The ACF field will return an array of values, you can see what this array looks like by

    
    $players = get_field('select_players');
    echo '<pre>'; print_r($players); echo '</pre>';
    

    You need to loop through the returned array and echo the parts that you want to display.

  • Thanks John, I can see the array but I’m having issues with the syntax to only display one value. I’ve got:

    <?php $players = get_field('select_players');
    foreach($players as $player) {
        echo $player['nickname'];
    } ?>

    But I still get the same issue of only the 1st character of each value is echo’d:
    CBtttt2D<

    It’s not selecting the [‘nickname’] only, but it’s doing ‘something’ because without that it prints the whole array. I hope that makes sense.

  • That’s kinda odd, can you post what this actually outputs

    
    echo '<pre>'; print_r($players); echo '</pre>';
    
  • This is the output @hube2

    Array
    (
        [ID] => 4
        [user_firstname] => Chris
        [user_lastname] => Burchell
        [nickname] => thrax
        [user_nicename] => thrax
        [display_name] => thrax
        [user_email] => thrax@playtopia.com.au
        [user_url] => 
        [user_registered] => 2018-01-02 04:18:15
        [user_description] => Dad, Gamer, Podcaster, and founder of playtopia.com.au!
        [user_avatar] => thrax
    )
  • Hi @hube2 – I’ve solves the issue 😀 It was with the ACF itself and me derping 🙂 Thanks for your assistance. The code was right all along *blush*

  • I am having the same problem as the OP was originally having, and I can’t seem to fix it, despite reading this thread a thousand times. Also note that I’m using FacetWP to display the listing page, which currently looks like this: https://cl.ly/0P3k250O2W1I

    The template in FacetWP currently contains this code: https://cl.ly/1x1U2B3Z1l1B

    I fail to see any problem with the code… how did you fix it?

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

You must be logged in to reply to this topic.