Support

Account

Home Forums General Issues Select field from user profile

Solved

Select field from user profile

  • I want to create a select field that is attached to the user role which, in itself is simple enough, but I’m unsure how to output this. Ideally, both label and value.

    The docs suggest the following;

    <?php
    $field = get_field_object( 'color' );
    $value = $field['value'];
    $label = $field['choices'][ $value ];
    ?>
    <p>Color: <span class="color-<?php echo esc_attr($value); ?>"><?php echo esc_html($label); ?></span></p>

    How would I now amend this so it works for fields within a user profile?

    I’m doing something like this for other fields;

    <?php 
                                            $repeater = get_field('invoices', 'user_' . $current_user->ID);
    
                                            $order = array();
    
                                            foreach( $repeater as $i => $row ) {
    
                                            $order[ $i ] = $row['invoice_number'];
    
                                            }
    
                                            array_multisort( $order, SORT_DESC, $repeater );
    
                                            if( $repeater ): ?>
    
                                            <?php foreach( $repeater as $i => $row ): ?>
                                            
                                            <tr>
    
                                                <td><?php echo $row['invoice_number']; ?></td>
                                                
                                                <td><?php echo $row['invoice_date']; ?></td>
                                                
                                                <td><?php echo $row['invoice_total']; ?></td>
                                                
                                                <td class="invoice-actions">
                                                    
                                                    <div>                                               
                                                    <a target="_blank" href="<?php echo $row['invoice']; ?>" ><i class="fa-regular fa-eyes"></i></a> 
                                                    
                                                    <a href="<?php echo $row['invoice']; ?>" download><i class="fa-regular fa-cloud-arrow-down"></i></a>  
                                                        
                                                    </div>
                                                </td>
                                                
                                            </tr>
    
                                            <?php endforeach; ?>
    
                                            <?php endif; ?>

    But I’m confused as to why this method doesn’t work on the select field.

  • You need to add the post ID to the get_field_object() call just like you’ve added it to this line in your other code.

    
    $repeater = get_field('invoices', 'user_' . $current_user->ID);
    
  • I’ve no idea why this didn’t work before, but it is working now.

    Thanks John 👍

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

You must be logged in to reply to this topic.