如果需要显示“选择选项”值而不是“键”,则必须为此使用选项回调函数。像这样,
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\'),
);
}
传递回调函数名称后,确保在cmb2\\u admin\\u init函数之外创建该函数,与示例相同。
现在,您只需显示select options键和值组合回调函数并获取\\u post\\u 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 />\';
我认为这是有道理的。