Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created a custom post type with an image gallery upload. Now I am trying to display the gallery on the front end. This is what I have so far that works to display 1 image, but if multiple images are uploaded all the URLs get stuck in the src tag. 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?

<?php if (have_posts()) :  while (have_posts()) : the_post(); ?>

<?php
echo '<img src="'.get_post_meta($post->ID, 'gallery-upload', true).'">';    
?>

<?php endwhile; else: ?>
 <p><?php _e('No posts were found. Sorry!'); ?></p>
<?php endif; ?>

EDIT:

This is what I ended up with that works...

<?php
foreach(get_post_meta($post->ID, 'gallery-upload') as $meta) {
 foreach(explode(',', $meta) as $src) {
  echo '<img src="'.htmlentities($src).'">';
 }
}
?>
share|improve this question
    
can you give an example of what WordPress prints out when you have more than one photo? –  guiniveretoo Oct 16 '12 at 4:07
    
Check out my edit above –  ftntravis Oct 16 '12 at 4:12
add comment

2 Answers

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.'">'; 
} 
?>
share|improve this answer
    
Thanks, I tried this but it produced the same results I had before. For some reason it isn't reproducing the img tags, it just spits all the data out in img tag. –  ftntravis Oct 16 '12 at 4:43
add comment

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).

share|improve this answer
    
Thank you for the response. I'll need to do some research. I used this tutorial here: krishnakantsharma.com/2012/01/… which is using Javascript to store the URL's. –  ftntravis Oct 16 '12 at 4:49
    
I noticed that it is being stored under "uploads" folder. So shouldn't this transfer over ok from development to it being live? I do agree that your answer looks like a better way, but I am unable to find how to rewrite how the JS is storing the URL. –  ftntravis Oct 16 '12 at 4:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.