0

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? Any help is appreciated.

<?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 is being returned:

<img src="http%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F800x400-volbeat-mock1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1574_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1576_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F1576_4_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F2244_2_1.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F300789_2349086884438_1168050047_32154880_1451576942_n.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F373795_278881222158106_278880528824842_834930_1454244548_n.jpeg%2Chttp%3A%2F%2Flocalhost%3A8888%2Fandreasmoulis%2Fwp-content%2Fuploads%2F2012%2F10%2F20110909-121141.jpeg">

1 Answer 1

1

Looking at the function reference, get_post_meta ordinarily returns an array unless the third argument is set to true. Something like this should work, more or less.

<?php
foreach(get_post_meta($post->ID, 'gallery-upload') as $meta) {
  foreach(explode(',', $meta) as $src) {
    echo '<img src="'.htmlentities($src).'">';
  }
}
?>

EDIT: Apparently gallery-upload is stored as comma-separated values. Updated my snippet above to hopefully account for this.

Sign up to request clarification or add additional context in comments.

5 Comments

I'll play around with it thanks. Check out my edit, that's what I got with your code. Any ideas why?
Heh, whoops. Remove the rawurlencode(...) function (see my update above) and it should fix it.
Still nothing, it's returning everything within one <img src=""> So it's producing all the links. Just not in separate image tags.
Ah! So looking at the $src it returned, it seems to be giving you a comma-separated list of values. Take a look at my updated snippet.
You are amazing good sir! I appreciate the help. Forgot about explode!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.