mywebsos / carbon-field-extended-select
Carbon Fields 的一个扩展,增加了具有检索标签、值或键值数组的 Select 字段的功能。
dev-main
2023-01-17 11:28 UTC
Requires
- php: >=7.4
- htmlburger/carbon-fields: ^3.3
This package is auto-updated.
Last update: 2024-09-17 13:57:44 UTC
README
此扩展向 Carbon Fields 库添加了一个 Select 字段,并增加了检索选中选项的标签、值或键值数组的功能。
安装
要安装此扩展,可以使用以下命令
composer require mywebsos/carbon-fields-extended-select
用法
要在您的 Carbon Fields 容器中使用扩展的 Select 字段,可以使用以下代码
use Carbon_Fields\Container;
use Carbon_Fields\Field;
Container::make( 'post_meta', __( 'Extended Select Field' ) )
->add_fields( array(
Field::make( 'extended_select', 'extended_select_field', __( 'Select options' ) )
->set_options( array(
'option_1' => 'Option 1',
'option_2' => 'Option 2',
'option_3' => 'Option 3',
) )
->return_format( 'array' ) // label, array or value ( default )
) );
您可以使用以下方法检索选中选项
// Retrieve the label|array|value of the selected option
$value = carbon_get_post_meta( get_the_ID(), $field_name );
注意
请确保在文件顶部包含 Carbon_Fields\Field 的使用声明。