Image inside clonable group not displayed in template
- This topic has 1 reply, 2 voices, and was last updated 6 years, 1 month ago by
Anh Tran.
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
March 7, 2019 at 7:15 PM #13593
---luke---
ParticipantHello
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!
LukasMarch 7, 2019 at 10:56 PM #13599Anh Tran
KeymasterHi 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. -
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.