Get group fields created with CMB2

I created a group of fields for an educational wordpress project using the plugin cmb2 and the following code:

<?php
add_action( ‘cmb2_init’, ‘cmb2_add_metabox_didattica’ );
function cmb2_add_metabox_didattica() {
$prefix = ‘_eduinaf_’;
$curriculum_scolastico = $cmb->add_field( array(
‘name’ => __( ‘Curriculum scolastico’, ‘eduinaf’ ),
‘id’ => $prefix . ‘curriculum_scolastico’,
‘type’ => ‘group’,
‘desc’ => __( ‘Cliccare su “Add Group” per aggiungere un nuovo gruppo di campi’, ‘eduinaf’ ),
‘options’ => array(
‘group_title’ => __( ‘Livello, materia, argomento’, ‘eduinaf’ ),
‘add_button’ => __( ‘Aggiungi un’altra materia’, ‘eduinaf’ ),
‘remove_button’ => __( ‘Rimuovi’, ‘eduinaf’ ),
‘sortable’ => true
)
) );
$cmb->add_group_field( $curriculum_scolastico, array(
‘name’ => __( ‘Livello scolastico’, ‘eduinaf’ ),
‘id’ => $prefix . ‘curriculum_scolastico_livello_scolastico’,
‘type’ => ‘text’,
) );
$cmb->add_group_field( $curriculum_scolastico, array(
‘name’ => __( ‘Materia’, ‘eduinaf’ ),
‘id’ => $prefix . ‘curriculum_scolastico_materia’,
‘type’ => ‘text’,
) );
$cmb->add_group_field( $curriculum_scolastico, array(
‘name’ => __( ‘Certificazione’, ‘eduinaf’ ),
‘id’ => $prefix . ‘curriculum_scolastico_certificazione’,
‘type’ => ‘text’,
) );
$cmb->add_group_field( $curriculum_scolastico, array(
‘name’ => __( ‘Argomento’, ‘eduinaf’ ),
‘id’ => $prefix . ‘curriculum_scolastico_argomento’,
‘type’ => ‘text’,
) );
}

So I create a shortcode to show the fields inside the group. Following this gist I write the following code:

<?php
add_shortcode(‘eduinaf-loop’, ‘eduinaf_loop’);
function eduinaf_loop( $attr = array() ) {
$post = get_post();
$curriculum = get_post_meta( $post->ID, ‘_eduinaf_curriculum_scolastico’, true );
$output = ”;

if ( empty( $curriculum ) ) {
return $output;
}

$output .= ‘<ul>’;

foreach ( $curriculum as $entry ) {
$output .= ‘<li>’;
$output .= ‘Livello scolastico: ‘ . esc_attr( $entry[‘_livello_scolastico’] ) . ‘</br>’;
$output .= ‘Materia: ‘ . esc_attr( $entry[‘_materia’] ) . ‘<br/>’;
$output .= ‘Argomento: ‘ . esc_attr( $entry[‘_argomento’] );
$output .= ‘</li>’;
}

$output .= ‘</ul>’;

return $output;
}

but it doesn’t work and I cannot find where is the problem (it seems equals to the starting example).

Any suggestions?

Thanks

Read more here:: Get group fields created with CMB2

Leave a Reply

Your email address will not be published. Required fields are marked *