Support » Plugin: CMB2 » Showing Repeatable Group

  • Resolved katojune

    (@katojune)


    Hello,

    I am trying to add repeatable fields but I can not get the group to display on the front end. For testing purposes I have added the example from the wiki to my theme but still nothing appears.

    The data saves in the backend and I can see see it in the database as well. I have other cmb2 fields that are working correctly so I’m kind of at a loss.

    Code I’m using

    $group_field_id = $cmb->add_field( array(
        'id'          => 'wiki_test_repeat_group',
        'type'        => 'group',
        'description' => __( 'Generates reusable form entries', 'cmb' ),
        'options'     => array(
            'group_title'   => __( 'Entry {#}', 'cmb' ), // since version 1.1.4, {#} gets replaced by row number
            'add_button'    => __( 'Add Another Entry', 'cmb' ),
            'remove_button' => __( 'Remove Entry', 'cmb' ),
            'sortable'      => true, // beta
            // 'closed'     => true, // true to have the groups closed by default
        ),
    ) );
    
    // Id's for group's fields only need to be unique for the group. Prefix is not needed.
    $cmb->add_group_field( $group_field_id, array(
        'name' => 'Entry Title',
        'id'   => 'title',
        'type' => 'text',
        // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
    ) );
    
    $cmb->add_group_field( $group_field_id, array(
        'name' => 'Description',
        'description' => 'Write a short description for this entry',
        'id'   => 'description',
        'type' => 'textarea_small',
    ) );
    
    $cmb->add_group_field( $group_field_id, array(
        'name' => 'Entry Image',
        'id'   => 'image',
        'type' => 'file',
    ) );
    
    $cmb->add_group_field( $group_field_id, array(
        'name' => 'Image Caption',
        'id'   => 'image_caption',
        'type' => 'text',
    ) );

    Front end code:

    <?php
    
    $entries = get_post_meta( get_the_ID(), 'wiki_test_repeat_group', true );
    
    foreach ( (array) $entries as $key => $entry ) {
    
        $img = $title = $desc = $caption = '';
    
        if ( isset( $entry['title'] ) )
            $title = esc_html( $entry['title'] );
    
        if ( isset( $entry['description'] ) )
            $desc = wpautop( $entry['description'] );
    
        if ( isset( $entry['image_id'] ) ) {
            $img = wp_get_attachment_image( $entry['image_id'], 'share-pick', null, array(
                'class' => 'thumb',
            ) );
        }
        $caption = isset( $entry['image_caption'] ) ? wpautop( $entry['image_caption'] ) : '';
    
        // Do something with the data
    
        echo $title;
        //print_r($entry);
    }
    ?>

    I only get blank line also when trying out print_r. Any help or direction would be appreciated.

    Thanks

    https://wordpress.org/plugins/cmb2/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’m not seeing any issues with this, to be honest.

    An example of the output I’m seeing from doing a var_dump() on $entries right before the foreach loop.

    array (size=2)
      0 =>
        array (size=5)
          'title' => string 'abc' (length=3)
          'description' => string 'def' (length=3)
          'image_id' => boolean false
          'image' => string 'http://ghi' (length=10)
          'image_caption' => string 'jkl' (length=3)
      1 =>
        array (size=4)
          'title' => string 'mno' (length=3)
          'description' => string 'pqr' (length=3)
          'image' => string 'http://stu' (length=10)
          'image_caption' => string 'vwx' (length=3)

    Something else must be the issue. When I run a var_dump I only returned
    string(0) “”

    I appreciate you taking the time to check.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Are you utilizing the prefix stuff that is typically used for CMB2 stuff? That’d change the meta keys used. It’s the prefix value + the ID value for each field.

    So it looks like that get_the_ID() was getting the wrong post ID and manually adding in the post ID got it to work.

    Thanks again for helping.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Good to hear you figured out the issue source. You’re welcome.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Showing Repeatable Group’ is closed to new replies.