-1

How can output select option name not ID?

$cmb->add_field(array(
    'name' => __('Age', 'cmb2'),
    'id' => $prefix . 'mpaa',
    'type' => 'select',
    'show_option_none' => true,
    'options' => array(
        'g' => __('option 1', 'cmb2'),
        'pg' => __('option 2', 'cmb2'),
        'pg13' => __('option 3', 'cmb2'),
        'r' => __('option 4', 'cmb2'),
        'nc17' => __('option 5', 'cmb2'),
    ),
));

I need it like Age : option 3 not Age : pg13

closed as off-topic by Mark Kaplun, fuxia Sep 15 '16 at 10:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

0

If you need to display select options value rather then key then you have to use an options callback function for this. like this,

add_action( 'cmb2_admin_init', 'your_function_name' );
function your_function_name() {
    $prefix = 'your_prifix_';
    /**
     * Sample metabox to demonstrate each field type included
     */
    $cmb = new_cmb2_box( array(
        'id'            => $prefix . 'metabox',
        'title'         => __( 'Title', 'cmb2' ),
        'object_types'  => array( 'post' ), // Post type
        'show_on' => array( ),
        // 'show_on_cb' => 'cm_show_if_front_page', // function should return a bool value
        // 'context'    => 'normal',
        // 'priority'   => 'high',
        // 'show_names' => true, // Show field names on the left
        // 'cmb_styles' => false, // false to disable the CMB stylesheet
        //'closed'     => true, // true to keep the metabox closed by default
    ) );

    $cmb->add_field(array(
        'name' => __('Age', 'cmb2'),
        'id' => $prefix . 'mpaa',
        'type' => 'select',
        'show_option_none' => true,
        // Use an options callback
        'options_cb' => 'show_cat_or_dog_options',
    ));

} // End of cmb2_admin_init function

function show_cat_or_dog_options() {
    // return a standard options array
    return array(
        'g' => __('option 1', 'cmb2'),
        'pg' => __('option 2', 'cmb2'),
        'pg13' => __('option 3', 'cmb2'),
        'r' => __('option 4', 'cmb2'),
        'nc17' => __('option 5', 'cmb2'),
    );
}

After pass the callback function name make sure you create that function outside of the cmb2_admin_init function same as example.

Now you just display your select options key and value combine callback function and get_post_meta();

$prefix = 'your_prifix_';
$options = show_cat_or_dog_options();
$key = get_post_meta( get_the_ID(), $prefix . 'mpaa', true );
$name = $options[ $key ];

echo 'Select Options Key/ID => ' . $key . '<br />';
echo 'Select Options Name/Value => ' . $name . '<br />';

I think it's make sense.

Not the answer you're looking for? Browse other questions tagged or ask your own question.