Select Field
The Select field provides a dropdown for choosing one option from a predefined list. Use it for categories, statuses, layout options, difficulty levels, or any single-choice selection.
Field Settings
| Setting | Description |
|---|---|
| Field Key | Unique identifier. Lowercase letters and underscores only. |
| Label | Display label shown above the field. |
| Description | Optional helper text shown below the field. |
| Choices | List of options. Each choice has a value (stored) and a label (displayed). |
| Default Value | Optional pre-selected choice. |
Return Value
Returns a string — the value (not the label) of the selected choice. Returns an empty string if nothing is selected.
Usage
php
// Get the value
$level = onemeta_get_meta( get_the_ID(), 'field_key' );
// Display the raw value
echo esc_html( $level );
// Map values to labels
$labels = [
'beginner' => 'Beginner',
'intermediate' => 'Intermediate',
'advanced' => 'Advanced',
];
echo esc_html( $labels[ $level ] ?? $level );
// Use in conditional logic
if ( $level === 'advanced' ) {
echo '<span class="badge badge--advanced">Advanced</span>';
}
Template Example
php
<?php $difficulty = onemeta_get_meta( get_the_ID(), 'difficulty' ); ?>
<?php if ( ! empty( $difficulty ) ) : ?>
<span class="difficulty difficulty--<?php echo esc_attr( $difficulty ); ?>">
<?php echo esc_html( ucfirst( $difficulty ) ); ?>
</span>
<?php endif; ?>
Notes
- The stored and returned value is the choice value, not the choice label
- Use the value for logic and CSS classes; map to a label array for display text