Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello Michael
    first i would like to thank you for your plugin

    i have a problem , i can’t display repeatable fields

    here is the repeatable fields group

    =================================================

    /**
    	 * Repeatable Field Groups
    	 */
    	$meta_boxes['field_group'] = array(
    		'id'           => 'field_group',
    		'title'        => __( 'Repeating Field Group', 'cmb2' ),
    		'object_types' => array( 'post', ),
    		'fields'       => array(
    			array(
    				'id'          => $prefix . 'repeat_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 works the same, except id's only need to be unique for this group. Prefix is not needed.
    				'fields'      => array(
    					array(
    						'name' => 'Entry Title',
    						'id'   => 'title',
    						'type' => 'text',
    						// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
    					),
    					array(
    						'name' => 'Description',
    						'description' => 'Write a short description for this entry',
    						'id'   => 'description',
    						'type' => 'textarea_small',
    					),
    					array(
    						'name' => 'Entry Image',
    						'id'   => 'image',
    						'type' => 'file',
    					),
    					array(
    						'name' => 'Image Caption',
    						'id'   => 'image_caption',
    						'type' => 'text',
    					),
    				),
    			),
    		),
    	);

    can i tell me how to display this code in single.php

    wlaedimir

    (@wlaedimir)

    You have to use this code in your template file

    <?php
    $entries = get_post_meta( get_the_ID(), $prefix . '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
    }
    ?>

    Then, you should print all the variables according how you want to display them in your template file.

    For example, after the foreach
    place something like this

    <div class="metabox">
    				          	<?php echo $img; ?>
    				          	<h3><?php echo $title; ?></h3>
    				            <p><?php echo $desc; ?></p>
    				            <p><?php echo $caption; ?></p>
    </div>
    <?php } ?>

    Hope this helps

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WDS Blog Post: CMB2 WordPress Plugin: What is it good for? Absolutely everything’ is closed to new replies.