Display repeatable field inside group
-
How to display repeatable fields inside a group?
Search this for hours, but can’t figure out.
Help docs are not very clear on this issue.custom fields:
/** * Repeatable Field Groups */ $cmb_group = new_cmb2_box( array( 'id' => 'metabox', 'title' => __( 'Repeating Field Group', 'cmb2' ), 'object_types' => array( 'discography', ), ) ); // $group_field_id is the field id string, so in this case: $prefix . 'demo' $group_field_id = $cmb_group->add_field( array( 'id' => $prefix . 'discography_group', 'type' => 'group', 'description' => __( 'Generates reusable form entries', 'cmb2' ), 'options' => array( 'group_title' => __( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number 'add_button' => __( 'Add Another Entry', 'cmb2' ), 'remove_button' => __( 'Remove Entry', 'cmb2' ), 'sortable' => true, // beta ), 'fields' => array( array( 'name' => __( 'Title', 'deejay' ), 'id' => 'track_title', 'type' => 'text' ), array( 'name' => __( 'Label', 'deejay' ), 'id' => 'track_label', 'type' => 'text' ), array( 'name' => __( 'Buy it on', 'deejay' ), 'id' => 'buy_link', 'type' => 'text', 'repeatable' => true ), ), ) );
for the front-end:
<?php $discography_group = get_post_meta( get_the_ID(), '_cmb2_discography_group', true ); ?> <ul> <?php foreach ( (array) $discography_group as $key => $entry ) { $label = $title = $buy = ''; if ( isset( $entry['track_label'] ) ) { $label = esc_html( $entry['track_label'] ); } if ( isset( $entry['track_title'] ) ) { $title = esc_html( $entry['track_title'] ); } if ( isset( $entry['buy_link'] ) ) { $buy = esc_html( $entry['buy_link'] ); } ?> <li> <h1><?php echo $discography_group [$key]['track_title'] ?></h1> <p><?php echo $discography_group [$key]['track_label'] ?></p> <a href="<?php echo $discography_group [$key]['buy_link'] ?>" target="_blank" class="float-right"></a> </li> <?php } ?> </ul>
thanks in advance for any help.
Viewing 11 replies - 1 through 11 (of 11 total)
Viewing 11 replies - 1 through 11 (of 11 total)
- The topic ‘Display repeatable field inside group’ is closed to new replies.