0

I am making frontand gallery uploader. Here is my example code:

<?php 
    //start of "for" loop
    $attach_id = media_handle_upload($key, $post_id);
    $attach_ids[] = $attach_id;
    add_post_meta($post_id, 'user_submit_image', wp_get_attachment_url($attach_id));
    //end of "for" loop
?>

And i am trying display them in admin post edit page with metabox.

<?php
function gallery_meta_callback($post) {
    wp_nonce_field(basename(__FILE__), 'gallery_meta_nonce');
    $ids = get_post_meta($post->ID, 'user_submit_image');

    ?>
    <table class="form-table">
      <tr><td>
        <a class="gallery-add button" href="#">Add Image(s)</a>

        <ul id="gallery-metabox-list">
        <?php if ($ids): foreach ($ids as $key => $value): $image = wp_get_attachment_image_src($value);?>

                  <li>
                    <input type="hidden" name="user_submit_image[<?php echo $key; ?>]" value="<?php echo $value; ?>">
                    <img class="image-preview" src="<?php echo $image[0]; ?>">
                    <a class="change-image" href="#" >Change Image</a><br>
                    <a class="remove-image" href="#">Remove Image</a>
                  </li>

                <?php endforeach;endif;?>
        </ul>

      </td></tr>
    </table>
  <?php }

function gallery_meta_save($post_id) {
    if (isset($_POST['user_submit_image'])) {
        update_post_meta($post_id, 'user_submit_image', $_POST['user_submit_image']);
    } else {
        delete_post_meta($post_id, 'user_submit_image');
    }
}
add_action('save_post', 'gallery_meta_save');
?>

And i must display on frontend:

<div class="gallery">
    <?php 
    $ids = get_post_meta($post->ID, 'user_submit_image');
    foreach ($ids as $key => $value) : $image = wp_get_attachment_image_src($value); ?>
        <div class="item">
        <img src="<?php echo $image[0]; ?>">
        </div>
    <?php endforeach; ?>                                                                        
</div>

But i cat do it :) Where do i make mistakes?

2
  • 1
    In add_post_meta() you're saving a URL, but in get_post_meta() you're treating the values as IDs. Commented Mar 1, 2018 at 6:36
  • i edited <img class="image-preview" src="<?php echo $value; ?>"> and it sorting images. Problem is when i try update_post_meta, it updates value as array()
    – wpdev
    Commented Mar 1, 2018 at 7:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.