Custom Fields
Re-Envisioned
Support Meta Box Group Image inside clonable group not displayed in template

This topic contains 1 reply, has 2 voices, and was last updated by  Anh Tran 6 months, 2 weeks ago.

  • Creator
    Topic
  • #13593

    —luke—
    Participant

    Hello
    I am trying to output following custom image fields inside a clonable group in my theme. I am using this code from your documentation. The text fields are displayed but not the image.

    This are my custom fields:

    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array (
            'title' => 'Gallerie',
            'id' => 'gallerie',
            'post_types' => array(
                0 => 'exhibitions',
            ),
            'context' => 'normal',
            'priority' => 'high',
            'fields' => array(
                array (
                    'id' => 'exhb_gallery_group',
                    'type' => 'group',
                    'name' => 'Bilder',
                    'fields' => array(
                        array (
                            'id' => 'exhb_gallery',
                            'type' => 'single_image',
                            'name' => 'Einzelbild',
                        ),
                        array (
                            'id' => 'exhb_gallery_description',
                            'name' => 'Bildunterschrift',
                            'type' => 'wysiwyg',
                            'options' => array(
                                'textarea_rows' => 2,
                                'media_buttons' => false,
                                'teeny' => true,
                            ),
                        ),
                    ),
                    'clone' => 1,
                    'sort_clone' => 1,
                    'default_state' => 'expanded',
                ),
            ),
        );
        return $meta_boxes;
    }

    This is my template code:

    // Group is cloneable
    $group_values = rwmb_meta( 'exhb_gallery_group' );
        //print_r($group_values);
    if ( ! empty( $group_values ) ) {
        foreach ( $group_values as $group_value ) {
            $image_ids = isset( $group_values['exhb_gallery'] ) ? $group_values['exhb_gallery'] : array();
            
            foreach ( $image_ids as $image_id ) {
                $image = RWMB_Image_Field::file_info( $image_id, array( 'size' => 'large' ) );
                echo '<img src="' . $image['url'] . '" width="' . $image['width'] . '" height="' . $image['height'] . '">';
                
                
            }
        
        echo '', $group_value['exhb_gallery_description'], '
    ';
        }
        
    }

    Thank you for your efforts!
    Lukas

Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #13599

    Anh Tran
    Keymaster

    Hi Lukas,

    As you use single_image field, the field value is not an array (as it’s only one image). So, please change to code to:

    $image_id = isset( $group_values['exhb_gallery'] ) ? $group_values['exhb_gallery'] : null;
    if ( $image_id ) {
        $image = RWMB_Image_Field::file_info( $image_id, array( 'size' => 'large' ) );
        echo '<img src="' . $image['url'] . '" width="' . $image['width'] . '" height="' . $image['height'] . '">';
    }

    Or if you really want to use a gallery, then please change the field type to image_advanced and keep the code as it is.

Viewing 1 replies (of 1 total)

You must be logged in to reply to this topic.