I used the @ brasofilo answer above, however I really skipped while preserving the image id, so I could use wp_get_attachment_image for example.
So here is my redesigned code that also saves the image id. I also use two arrays to determine which page templates and, accordingly, which mailbox a meta window appears on. Here is the code:
<?php add_action( 'admin_init', 'add_post_gallery_so_14445904' ); add_action( 'add_meta_boxes_page', 'add_page_gallery_so_14445904' ); add_action( 'admin_head-post.php', 'print_scripts_so_14445904' ); add_action( 'admin_head-post-new.php', 'print_scripts_so_14445904' ); add_action( 'save_post', 'update_post_gallery_so_14445904', 10, 2 ); <div id="dynamic_form"> <div id="field_wrap"> <?php if ( isset( $gallery_data['image_url'] ) ) { for( $i = 0; $i < count( $gallery_data['image_url'] ); $i++ ) { ?> <div class="field_row"> <div class="field_left"> <div class="form_field"> <input type="hidden" class="meta_image_url" name="gallery[image_url][]" value="<?php esc_html_e( $gallery_data['image_url'][$i] ); ?>" /> <input type="hidden" class="meta_image_id" name="gallery[image_id][]" value="<?php esc_html_e( $gallery_data['image_id'][$i] ); ?>" /> </div> <div class="form_field" style="margin-bottom: 20px"> <label>Description</label> <textarea class="meta_image_desc" name="gallery[image_desc][]" rows="3" style="width: 100%"><?php esc_html_e( $gallery_data['image_desc'][$i] ); ?></textarea> </div> <input class="button" type="button" value="Choose File" onclick="add_image(this)" /> <input class="button" type="button" value="Remove" onclick="remove_field(this)" /> </div> <div class="field_right image_wrap"> <img src="<?php esc_html_e( $gallery_data['image_url'][$i] ); ?>" /> </div> <div class="clear" /></div> </div> <?php } </div> <div style="display:none" id="master-row"> <div class="field_row"> <div class="field_left"> <div class="form_field"> <input class="meta_image_url" value="" name="gallery[image_url][]" /> <input class="meta_image_id" value="" name="gallery[image_id][]" /> </div> <div class="form_field" style="margin-bottom: 20px"> <label>Description</label> <textarea class="meta_image_desc" name="gallery[image_desc][]" rows="3" style="width: 100%"></textarea> </div> <input type="button" class="button" value="Choose Image" onclick="add_image(this)" /> <input class="button" type="button" value="Remove" onclick="remove_field(this)" /> </div> <div class="field_right image_wrap"> </div> <div class="clear"></div> </div> </div> <div id="add_field_row"> <input class="button" type="button" value="Add Image" onclick="add_field_row();" /> </div> <?php if ( 'trend' == get_post_type( $post->ID ) ) { ?> <p style="color: #a00;">Make sure the number if images you add is a <b>multiple of 5</b>.</p> <?php } ?> </div> <?php } function print_scripts_so_14445904() { <style type="text/css"> .field_left { float:left; width: 75%; padding-right: 20px; box-sizing:border-box; } .field_right { float:left; width: 25%; } .image_wrap img { max-width: 100%; } #dynamic_form input[type=text] { width:100%; } #dynamic_form .field_row { border:1px solid #cecece; margin-bottom:10px; padding:10px; } #dynamic_form label { display: block; margin-bottom: 5px; } </style> <script type="text/javascript"> function add_image(obj) { var parent=jQuery(obj).parent().parent('div.field_row'); var inputField = jQuery(parent).find("input.meta_image_url"); var inputFieldID = jQuery(parent).find("input.meta_image_id"); var fileFrame = wp.media.frames.file_frame = wp.media({ multiple: false }); fileFrame.on('select', function() { var selection = fileFrame.state().get('selection').first().toJSON(); inputField.val(selection.url); inputFieldID.val(selection.id); jQuery(parent) .find("div.image_wrap") .html('<img src="'+selection.url+'" />'); }); fileFrame.open(); </script> <?php } function update_post_gallery_so_14445904( $post_id, $post_object ) {
Here is an example of how you can output data to your topic.
<?php if ( '' != get_post_meta( get_the_ID(), 'gallery_data', true ) ) { $gallery = get_post_meta( get_the_ID(), 'gallery_data', true ); } if ( isset( $gallery['image_id'] ) ) : for( $i = 0; $i < count( $gallery['image_id'] ); $i++ ) { if ( '' != $gallery['image_id'][$i] ) { echo wp_get_attachment_image( $gallery['image_id'][$i], 'gallery_large' ); if ( isset($gallery['image_desc'][$i]) ) echo $gallery['image_desc'][$i]; } } endif; ?>
source share