2

I have a php loop that loads some images onto some slider:

<div id="slider" class="flexslider">
<ul class="slides">

foreach($imglist as $image) {

$caption =  // I am populating this variable from the image meta data

echo '<li><img src="'.$img_folder.$image.'">';
}
</ul>
</div>
<div id="caption-block" class="myNewDiv">
<p>
echo $caption;
</p>
</div>

I am also pulling meta data from the image and loading each meta data. My question has to do with the fact that I would like to load the caption outside of the in which the loop resides onto a separate . As you can probably infer, the code I have written does not work because $caption is not dynamically loaded as it's not inside the loop.

Is it possible to have the $caption which is on a separate div be populated dynamically with php? If so, what's the best approach?

5
  • Maybe I'm misunderstanding you, but I think you want to change $caption = to $image Commented Jul 25, 2014 at 22:26
  • Sorry perhaps I misguided, I will work on further elaborating the question. What I want to do is populate the $caption inside the <div class="myNewDiv">, which is outside of the loop that initializes that variable. Commented Jul 25, 2014 at 22:29
  • You'll need to output the HTML in the loop as well. Commented Jul 25, 2014 at 22:31
  • I see, I was thinking that'd be the case, but I thought it wouldn't hurt to ask. Thanks for your assistance. Commented Jul 25, 2014 at 22:34
  • Is the caption just a single element and the variable a string? If so I don't see what the problem is? Commented Jul 25, 2014 at 22:35

1 Answer 1

1

You can use javascript to dynamically inject the caption into the #caption-block div using a Flexslider callback. In that case, you could store the caption in a data attribute of the img tag and then pull it out using a custom function in the Flexslider before: callback. Here's the HTML/PHP:

<?php echo '<li><img src="'.$img_folder.$image.'" data-caption="'.$caption.'">'; ?>

Inside the Flexslider before: callback:

before: function(slider){
    // set current slide 
    var currSlide = slider.slides.eq(slider.animatingTo + 1);
    var caption = $(currSlide).find("img").data('caption');
    $('#caption-block p').text(caption);
}
Sign up to request clarification or add additional context in comments.

Comments

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.