WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a slideshow which is populated with background images from a custom post type. Here's my code:

// start the loop
foreach($slider_posts as $post) : setup_postdata($post);
// get image
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'home-slide');

<script type="text/javascript">

//              containment:"#wrapper",

                    "<?php echo $thumbnail[0]; ?> "

<?php endforeach; ?>
<?php wp_reset_postdata(); ?>

<?php } ?>

This code displays just one slide image and not all the slide's images!

How can I fix it?

share|improve this question
I don't think your script should be inside the foreach, but that the foreach should be inside the script. – Shane Feb 22 '12 at 15:55

Try moving your foreach into the script tag, as such :

In the images parameter of your javascript :


      foreach( $slider_posts as $post )

      $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'home-slide');

      echo '"' . $thumb[0] . '",'; //Assuming images are separated by a comma



You may need to alter the example a bit to get it working properly.

Meta Example

 foreach( $slider_posts as $post )
      $meta = get_post_meta( $post->ID, 'the-meta-field-name', true ); //replace true with false if you want an array of meta field results
share|improve this answer
GREAT!!!!!!!!!!!!!!!!!!!!!!!!! is working!!! THANKS Shane! – user13401 Feb 22 '12 at 17:12
No problem. Could you just accept the answer so the question can appear as resolved. – Shane Feb 22 '12 at 17:46
just last question: To add post meta data from custom field fo each slide? – user13401 Feb 22 '12 at 19:10
inside the foreach, use get_post_meta function. I'll add an example. Codex link : codex.wordpress.org/Function_Reference/get_post_meta – Shane Feb 22 '12 at 19:16
sorry !thi code is correct to have a slideshow with text (with meta field)?? thanks! <?php foreach( $slider_posts as $post ) { $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'home-slide'); $meta = get_post_meta( $post->ID, 'the-meta-field-name', true ); echo '"' . $thumb[0] . '",' echo '"' . $meta[0] . '",'; //Assuming images are separated by a comma } ?> – user13401 Mar 7 '12 at 17:46

Your Answer


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.