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 a slideshow which is populated with background images from a custom post type. Here's my code:

<?php
// 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">
        $(function(){

            $.mbBgndGallery.buildGallery({
//              containment:"#wrapper",
                containment:"body",
                timer:5000,
                effTimer:5000,
                controls:"#controls",
                grayScale:false,
                autoStart:true,         

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

            });
        });
    </script>
<?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
1  
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
add comment

1 Answer

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

In the images parameter of your javascript :

 //[...]
 images:[
      <?php

      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
show 1 more 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.