Leave a Reply

3 comments

  1. You should reorganize the way you store the images: Make the uploaded files children of that particular post, do not put them in a post meta field. Then get all the images with get_children(). Look at the built-in handler for the [gallery] shortcode for some examples.

    I should go like this:

    $args = array( 
       'post_mime_type' => 'image',
       'numberposts'    => -1,
       'post_parent'    => get_the_ID(),
       'post_type'      => 'attachment' 
    );
    
    $attached_images = get_children( $args );
    
    foreach ( $attached_images as $image )
    {
        // print image
    }
    

    And even if you want to stay with post meta fields, do not store URLs, use the attachment IDs instead. URLs can change any time (think about a migration from dev to production site).

  2. So I’m guessing I should loop through that array and spit out each one separately? Would that be the route to go and if so how can I accomplish this?

    Yes. get_post_meta(...) should return an array without that last parameter, or with it set to false. You’d then have…

    <?php
    $meta = get_post_meta($post->ID, 'gallery-upload');
    foreach ($meta as $m) {
        echo '<img src="'.$m.'">'; 
    } 
    ?>
    
  3. Get images from Gallery Custom Post Meta

      <?php 
        $meta = get_post_meta(get_the_ID(), 'gallery');
        foreach ($meta as $m) :
        $img_atts = wp_get_attachment_image_src( $m, $default );
        $img_src = $img_atts[0];
      ?>
            <div class="swiper-slide"><img src="<?php echo $img_src ?>" alt=""></div>
    
      <?php endforeach ?>