I’m using Custom Metaboxes 2 and I can’t get repeatable group values to display on the front end. I have a repeatable group with a single text field.
I have the backend code first, followed the front end code.
add_action( 'cmb2_init', 'awc_register_repeatable_group_field_metabox' ); function awc_register_repeatable_group_field_metabox() { // Start with an underscore to hide fields from custom fields list $prefix = '_awc_'; /** * Repeatable Field Groups */ $cmb_group = new_cmb2_box( array( 'id' => $prefix . 'songs', 'title' => __( 'Track Listing', 'cmb2' ), 'object_types' => array( 'awc_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 . 'demo', 'type' => 'group', //'description' => __( 'Generates reusable form entries', 'cmb2' ), 'options' => array( 'group_title' => __( 'Track {#}', 'cmb2' ), // {#} gets replaced by row number 'add_button' => __( 'Add Another Track', 'cmb2' ), 'remove_button' => __( 'Remove Track', 'cmb2' ), 'sortable' => true, // beta ), ) ); /** * Group fields works the same, except ids only need * to be unique to the group. Prefix is not needed. * * The parent field's id needs to be passed as the first argument. */ $cmb_group->add_group_field( $group_field_id, array( 'name' => __( 'Title', 'cmb2' ), 'id' => 'title', 'type' => 'text', ) ); }
FRONT END:
$entries = get_post_meta( get_the_ID(), $prefix . 'songs', true ); foreach ( (array) $entries as $key => $entry ) { $songtitle = ''; if ( isset( $entry['demo'] ) ) $title = esc_html( $entry['demo'] ); // Do something with the data echo $title; // Don't know if this is the correct method. }
Advertisement
Answer
Do you have $prefix
defined in your front end code? That’s the only thing I can see being wrong there. If $prefix
isn’t defined, you’re looking up the post_meta with key songs
instead of _awc_songs
.