Skip to content

Groups in a row  #38

@s1dekick223

Description

@s1dekick223

Hey,

i'm a php rookie. I need to have a group field with 2 boxes beside each other my actual result looks like this: https://gyazo.com/3ad285902e3d68115eb044bf1cf949a2 But i need the two boxes in a row.

Pls help me thank you.

Lasko

This is my Code for this. Feel free to give me tips and advices

<?php

add_action( 'cmb2_admin_init', 'pb_Metabox1' );


function pb_metabox1() {
    
    $prefix      = '_yourprefix_group_';
            $cmb_group   = new_cmb2_box(array(
                'id'             => $prefix . 'metabox',
                'title'          => __('Repeating Field Group', 'cmb2'),
                'object_types'   => array('rezepte',),
            ));
            $field1      = $cmb_group->add_field(array(
                'name'   => __('Test Text', 'cmb2'),
                'desc'   => __('field description (optional)', 'cmb2'),
                'id'     => $prefix . 'text',
                'type'   => 'text',
            ));
            $field2      = $cmb_group->add_field(array(
                'name'   => __('Test Text Small', 'cmb2'),
                'desc'   => __('field description (optional)', 'cmb2'),
                'id'     => $prefix . 'textsmall',
                'type'   => 'text',
            ));

            // $group_field_id is the field id string, so in this case: $prefix . 'demo'
            $group_field_id  = $cmb_group->add_field(array(
                'id'         => $prefix . 'demo',
                'type'       => 'group',
                'options'    => array(
                    'group_title'    => __('Entry {#}', 'cmb2'), // {#} gets replaced by row number
                    'add_button'     => __('Add Another Entry', 'cmb2'),
                    'remove_button'  => __('Remove Entry', 'cmb2'),
                    'sortable'       => true,
                ),
            ));
            $gField1         = $cmb_group->add_group_field($group_field_id, array(
                'name'   => __('Entry Title', 'cmb2'),
                'id'     => 'title',
                'type'   => 'text_email',
            ));
            $gField2         = $cmb_group->add_group_field($group_field_id, array(
                'name'           => __('Description', 'cmb2'),
                'description'    => __('Write a short description for this entry', 'cmb2'),
                'id'             => 'description',
                'type'           => 'text_email',
            ));
    
           if (!is_admin()) {
                return;
            }

            //Create a default grid
            $cmb2Grid = new \Cmb2Grid\Grid\Cmb2Grid($cmb_group);

            //Create now a Grid of group fields
            $cmb2GroupGrid   = $cmb2Grid->addCmb2GroupGrid($group_field_id);
            $row             = $cmb2GroupGrid->addRow();
            $row->addColumns(array(
                $gField1, $gField2
            ));

            //Now setup your columns like you generally do, even with group fields
            $row = $cmb2Grid->addRow();
            $row->addColumns(array(
                $field1, $field2
            ));
            $row = $cmb2Grid->addRow();
            $row->addColumns(array(
                $cmb2GroupGrid // Can be $group_field_id also
            ));
    
   
    
    
    
    
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions