acato-plugins/cmb2-flexible-content 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

acato-plugins/cmb2-flexible-content

Composer 安装命令:

composer require acato-plugins/cmb2-flexible-content

包简介

A flexible content field for CMB2.

README 文档

README

The CMB2 Flexible Field allows the creation of group layouts, which can then be selected and switched between by a user. Each layout group contains a set of existing CMB2 fields, defined in an array in the field setup. These layout groups are then toggleable, sortable and repeatable.

Screenshot of UI

The data itself is saved to the field's ID in a serialized and iterable array with the layout group ID attached.

Adding a Flexible Field

Adding a flexible field is the same as adding any other CMB2 field. The only addition is the layouts option, which should contain an array of arrays, each with a separate group.

To begin, set up a standard CMB2 metabox:

// Basic CMB2 Metabox declaration
$cmb = new_cmb2_box( array(
	'id'           => 'prefix-metabox-id',
	'title'        => __( 'Flexible Content Test' ),
	'object_types' => array( 'post', ),
) );

Then add your flexible field definition. Each layout group should be defined in the layouts array, with the `ID` for that group as its key. Each layout group can contain a `title` and a list of CMB2 `fields`.

// Sample Flexible Field
$cmb->add_field( array(
	'name'       => __( 'Test Flexible', 'cmb2-flexible' ),
	'desc'       => __( 'field description (optional)', 'cmb2-flexible' ),
	'id'         => 'prefix_flexible',
	'type'       => 'flexible',
	'layouts' => array(
			'text' => array(
				'title' => 'Text Group',
				'fields' => array(
					array(
						'type' => 'text',
						'name' => 'Title for Text Group',
						'id' => 'title',
					),
					array(
						'type' => 'textarea',
						'name' => 'Description for Text Group',
						'id' => 'description',
					)
				),
			),
			'image' => array(
				'title' => 'Image Group',
				'fields' => array(
					array(
						'type' => 'file',
						'name' => 'Image for Image Group',
						'id' => 'title',
					),
					array(
						'type' => 'textarea',
						'name' => 'Description for Image Group',
						'id' => 'description',
					)
				),
			),
		)
) );

Getting Data from a Flexible Field

Flexible fields are stored in a single meta key, and can be retrieved using the WordPress API. This will return an array of data, each with the layout key defined as layout and the group's data by field ID.

$flexible_fields = get_post_meta( $post_id, 'flexible_field_name', true );
foreach( $flexible_fields as $field ) {
    if ( 'text' === $field['layout'] ) { ?>
        <h2><?php echo esc_html( $field['title'] ); ?></h2>
        <?php echo esc_html( $field['description'] );
    }
}

统计信息

  • 总下载量: 543
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2026-03-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固