Skip to content

Select

A dropdown select field for choosing a single option from a predefined list.

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

SettingDescription
Field KeyUnique identifier. Lowercase letters and underscores only.
LabelDisplay label shown above the field.
DescriptionOptional helper text shown below the field.
ChoicesList of options. Each choice has a value (stored) and a label (displayed).
Default ValueOptional 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

On This Page