• Buenos días,
    Tengo un pequeño problema con cmb2 repeatable group field metabox.
    Después de añadir los campos y valores no veo nada en la pantalla ni un mensaje de error .
    Aquí tengo el código:

    add_action( 'cmb2_admin_init', 'aa_projects_register_repeatable_group_field_metabox' );
        /**
         * Hook in and add a metabox to demonstrate repeatable grouped fields
         */
        function aa_projects_register_repeatable_group_field_metabox() {
         
            // Start with an underscore to hide fields from custom fields list
            $prefix = '_aa_projects_group_';
         
            /**
             * Repeatable Field Groups
             */
            $cmb_group = new_cmb2_box( array(
                'id'           => $prefix . 'metabox',
                'title'        => __( 'Repeating Field Group', 'farmacias' ),
                'object_types'  => array( 'post', 'farmacias' ), // Post type 
            ) );
         
            // $group_field_id is the field id string, so in this case: $prefix . 'demo'
            $group_field_id = $cmb_group->add_field( array(
                'id'          => $prefix . 'slides',
                'type'        => 'group',
                'description' => __( 'Generates reusable form entries', 'farmacias' ),
                'options'     => array(
                    'group_title'   => __( 'Entry {#}', 'farmacias' ), // {#} gets replaced by row number
                    'add_button'    => __( 'Add Another Entry', 'farmacias' ),
                    'remove_button' => __( 'Remove Entry', 'farmacias' ),
                    'sortable'      => true, // beta
                    // 'closed'     => true, // true to have the groups closed by default
                ),
            ) );
         
               
            $cmb_group->add_group_field( $group_field_id, array(
                'name'       => __( 'Titel', 'farmacias' ),
                'id'         => 'title',
                'type'       => 'text',
                // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
            ) ); 
        }

    ================ En mi página ======================

    
    <?php 
    if(have_posts()): while(have_posts()): the_post(); 
    $entries = get_post_meta( get_the_ID(), 'metabox', true );
    foreach ( (array) $entries as $key => $entry ) {
     
        if ( isset( $entry['title'] ) )
            $title = esc_html( $entry['title'] );
         
        // Output the values
        if ( !empty($title) ) echo '<h2>'.$title.'</h2>';
    }
    
    ?>
      <?php endwhile; else : ?>
    	<h3>Sorry we could not find what you are looking for </h3>
    <?php endif; ?> 

    Gracias

    • Este debate fue modificado hace 6 años, 10 meses por webamies.
Viendo 2 respuestas - de la 1 a la 2 (de un total de 2)
  • Moderador LGrusin

    (@lgrusin)

    Según la documentación si $prefix = '_aa_projects_group_';
    la forma de leer sería
    get_post_meta( get_the_ID(), '_aa_projects_group_xxx', true );

    https://github.com/CMB2/CMB2/wiki/Basic-Usage

    Un saludo

    Iniciador del debate webamies

    (@webamies)

    LGrusin Gracias por tu comentario,
    Ahora estoy intentado con el mismo ejemplo de la documentación, y sigo sin recuperar los datos.

    =============== Metabox =================

    add_action( 'cmb2_admin_init', 'sanidad_register_repeatable_group_field_metabox' );
    /**
     * Hook in and add a metabox to demonstrate repeatable grouped fields
     */
    function sanidad_register_repeatable_group_field_metabox() {
    
     $prefix = 'sanidad_group_';
    
    /**
         * Repeatable Field Groups
         */
        $cmb_group = new_cmb2_box( array(
            'id'           => $prefix . 'metabox',
            'title'        => esc_html__( 'Repeating Field Group', 'cmb2' ),
             'object_types'  => array( 'post', 'farmacias' ), // Post type 
        ) );
    
        // $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' => esc_html__( 'Generates reusable form entries', 'cmb2' ),
            'options'     => array(
                'group_title'   => esc_html__( 'Entry {#}', 'cmb2' ), // {#} gets replaced by row number
                'add_button'    => esc_html__( 'Add Another Entry', 'cmb2' ),
                'remove_button' => esc_html__( 'Remove Entry', 'cmb2' ),
                'sortable'      => true,
                // 'closed'     => true, // true to have the groups closed by default
            ),
        ) );
    
        /**
         * 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'       => esc_html__( 'Entry Title', 'cmb2' ),
            'id'         => 'title',
            'type'       => 'text',
            // 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
        ) );
    
        $cmb_group->add_group_field( $group_field_id, array(
            'name'        => esc_html__( 'Description', 'cmb2' ),
            'description' => esc_html__( 'Write a short description for this entry', 'cmb2' ),
            'id'          => 'description',
            'type'        => 'textarea_small',
        ) );
    
        $cmb_group->add_group_field( $group_field_id, array(
            'name' => esc_html__( 'Entry Image', 'cmb2' ),
            'id'   => 'image',
            'type' => 'file',
        ) );
    
        $cmb_group->add_group_field( $group_field_id, array(
            'name' => esc_html__( 'Image Caption', 'cmb2' ),
            'id'   => 'image_caption',
            'type' => 'text',
        ) );
    
    }

    ================ Codigo de recuparción de datos ====================

    <?php 
    if(have_posts()): while(have_posts()): the_post(); 
     
    $entries = get_post_meta( get_the_ID(), 'demo', 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
    }
    ?>
    
      <?php endwhile; else : ?>
    	<h3>Sorry we could not find what you are looking for </h3>
    <?php endif; ?>      

    porfa ayudame.

    un saludo,

Viendo 2 respuestas - de la 1 a la 2 (de un total de 2)
  • El debate ‘CMB2 repeatable group field metabox’ está cerrado a nuevas respuestas.